Linq EF-最適化されたクエリを使用して、特定の顧客のすべてのアカウント、カード、ローンなどをデータベースから収集する方法は?

ロバートダニエルソン

データベースから特定の顧客のすべての情報を収集しようとしていますが、データベースに数回クエリを実行せずに、最適化されたクエリですべての情報を収集する方法がわかりません。

私は次の表を持っています:https//i.imgur.com/o9PRrF1.png

私が欲しいのは、1人の顧客を提供されたCustomerIdと一致させることです。次に、そのアカウントに関連するすべてのカード、アカウント、ローン、および永久注文を収集します。

私はなんとかそれを行うことができましたが、私の_contextにいくつかのクエリを使用しています。複数のテーブルを結合するときに最適化されたクエリを実行する方法を学びたいです。

Linqの経験が豊富な人が、CustomerIdが「1」の顧客に関連するすべてのカード、アカウント、ローン、および永久注文を収集するクエリの例を提供できますか?

いくつかのヒントを得て、これを支援していただければ幸いです。最適化されたクエリを実行する方法を知ることは、非常に重要なスキルです。どうもありがとう!:-)

私が自分で試したことの例:

model.Customer = await _context.Customers.SingleOrDefaultAsync(c => c.CustomerId == request.CustomerId);

        model.Accounts = await (from acc in _context.Accounts
                                join disp in _context.Dispositions on acc.AccountId equals disp.AccountId
                                where disp.CustomerId == request.CustomerId
                                select acc).ToListAsync();

        model.Cards = await (from card in _context.Cards
                             join disp in _context.Dispositions on card.DispositionId equals disp.DispositionId
                             where disp.CustomerId == request.CustomerId
                             select card).ToListAsync();

これが私がデータで埋めようとしている私のビューモデルです:

 public class GetCustomerDetailsViewmodel
{
    public Customer Customer { get; set; }
    public List<Account> Accounts { get; set; } = new List<Account>();
    public decimal TotalBalance { get; set; }
    public List<Card> Cards { get; set; } = new List<Card>();
    public List<PermenentOrder> PermantentOrders { get; set; } = new List<PermenentOrder>();
    public List<Loan> Loans { get; set; } = new List<Loan>();

}

顧客は、処分のリスト、顧客間のリンクテーブル-アカウントとカードを持っています。

**Customers**
PK CustomerId
public virtual ICollection<Disposition> Dispositions { get; set; }

 **Cards**:
PK public int CardId { get; set; }
FK public int DispositionId { get; set; }
public virtual Disposition Disposition { get; set; }

**Dispositions**:
PK public int DispositionId { get; set; }
FK public int CustomerId { get; set; }
   public int AccountId { get; set; }
public virtual Account Account { get; set; }
public virtual Customer Customer { get; set; }
public virtual ICollection<Card> Cards { get; set; }

**Accounts**:
PK public int AccountId { get; set; }
public virtual ICollection<Disposition> Dispositions { get; set; }
public virtual ICollection<Loan> Loans { get; set; }
public virtual ICollection<PermenentOrder> PermenentOrder { get; set; }
public virtual ICollection<Transaction> Transactions { get; set; }

**Loans**
PK public int LoanId { get; set; }
public virtual Account Account { get; set; }

**PermenentOrder**
PK public int OrderId { get; set; }
FK public int AccountId { get; set; }
public virtual Account Account { get; set; }
マットJ

最も単純なクエリは、ステートメントが大きくなるにつれて単一のlinqステートメントに詰め込むのではなく、個別にクエリを実行することです。linqはますます非効率的になります。

遅延読み込みでは、

あなたは次のような簡単なことをすることができます、

var model = new GetCustomerDetailsViewmodel();
model.Customer = context.Customers.SingleOrDefault(c => c.CustomerId == id);
if (model.Customer != null)
{
     model.Accounts = model.Customer.Dispositions.Select(x => x.Account).ToList();
     model.Cards = model.Customer.Dispositions.SelectMany(x => x.Cards).ToList();
     model.PermantentOrders = model.Accounts.SelectMany(x => x.PermenentOrder).ToList();
}

遅延読み込みなしで、

単一のクエリですべてをロードする必要があります。これは効率的なクエリではない可能性があることに注意してください。Linqは効率性ではなく、便利さと書きやすさを重視しています。

var customerProfile = context.Customers.Where(x => x.CustomerId == id).Select(x => new
        {
            Customer = x,
            Accounts = x.Dispositions.Select(d => d.Account),
            Cards = x.Dispositions.SelectMany(d => d.Cards).ToList(),
            PermanentOrders = x.Dispositions.SelectMany(d => d.Account.PermenentOrder),
        }).FirstOrDefault();

        if (customerProfile != null)
        {
            var model = new GetCustomerDetailsViewmodel();
            model.Customer = customerProfile.Customer;
            model.Accounts = customerProfile.Accounts.ToList();
            model.Cards = customerProfile.Cards.ToList();
            model.PermantentOrders = customerProfile.PermanentOrders.ToList();
        }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ