开发者

LINQ query to a custom class

开发者 https://www.devze.com 2023-02-22 09:01 出处:网络
I have a class which has the following fields: public Account account; public IEnumerable<PurchaseTransaction> purchases;

I have a class which has the following fields:

public Account account;
public IEnumerable<PurchaseTransaction> purchases;

Basically, I want to select each 'Account', and per开发者_如何学Goform an outer join to the purchasetransactions table, and select all the purchases and place them in the 'purchases' IEnumerable (or leave empty if there's no purchases), and the Account in the 'account' field. How easy is this to do?


This is where GroupBy will come in handy. It automatically creates the IEnumerable for you also!

Example:

from row in db.purchases
group row by row.AccountID into g
select g;

g is already an IEnumerable containing all the purchases for the account ID which is stored in g.Key.


Solved by joining onto the purchases then selecting them into a new instance of my class.

0

精彩评论

暂无评论...
验证码 换一张
取 消