2011-09-08 94 views
0

架构已根据我更改,因此用户FK在条目表不再,但通过账目表间接引用Linq查询遍历表

我有以下表格: 用户 帐户(FK从用户) 项(FK从账户)

我基本上想要得到的条目使用LINQ从一个特定的用户:

var userAccounts = context.User.Include("Accounts").Include("Entries").Where(s => s.UserId == userId); 

这是以前的查询之前用户从条目中删除:

return (from pc in userAccounts 
    select new Entry{ 
     Timestamp = pc.Timestamp, 
     Log = pc.Log }).ToList().AsQueryable(); 

只是不知道该如何遍历表并填充条目类型

回答

1

如果您更喜欢基于关联的方法,而不是加入,考虑以下因素:

var userAccounts = from user in context.User 
        where user.UserId == userId 
        from account in user.Accounts 
        from entry in account.Entries 
        select entry; 
+0

竖起大拇指可读性 –

0

,您可以加入用户账户的条目表,让你可以与用户id的所有条目。

类似的东西:

from ua in userAccounts join en in entries on ua.accountId equals en.accountId into data 
where data.userId = someId 
select data.entries 

希望这会有所帮助。