2011-12-21 138 views
3

FollowingUsers如何编写LINQ查询?

StatusUpdates

上面可以看到FollowingUsersStatusUpdates表。

FollowingUsers,我存储FollowerUsernameFollowingUsername。 在StatusUpdates,我店Status updates的用户。

下面你可以看到我写的检索谁在登录的用户的状态更新原始查询。

var list = new List<StatusUpdate>(); 
list = (from x in db.StatusUpdates 
     where x.Author == User.Identity.Name 
     orderby x.Timestamp descending 
     select x) 
     .Take(count).ToList(); 

如何从以下获取状态更新的登录用户?

回答

2

以下应该工作,虽然我没有你的数据库来测试它。请注意,在调用ToList之前它不会被执行,因此所有事情都应该在单个数据库查询中发生。此外,不需要创建新列表,因为它将被您的查询覆盖,所以我已经整理了一下。

var Users = from f in db.FollowingUsers 
      where f.FollowerId == User.Identity.Name 
      select f.FollowingId; 

var list = (from x in db.StatusUpdates 
      from y in Users 
      where x.Author == y 
      orderby x.Timestamp descending 
      select x) 
      .Take(count).ToList(); 
+0

谢谢Tuskan360。但它没有工作:( – ChamingaD 2011-12-21 18:36:21

+2

你能更具体吗?什么没有工作?你得到一个错误信息吗?它编译吗?你刚刚得到错误的数据吗? – ForbesLindesay 2011-12-21 19:14:04

+0

另外,你是否使用查询SQL数据库LINQ to SQL?或其他东西? – ForbesLindesay 2011-12-21 19:14:37