如何基于Id向LINQ查询添加条件

时间:2016-01-05 09:08:42

标签: c# asp.net-mvc linq

这是我的操作方法,用于获取所有用户的ID。

public JsonResult GetUsers()
 {
  var ret = (from user in db.Users.ToList()
              select new 
               {
                UserName = user.UserName,
 // i am stuck here, i want to get all those ids whom current logged  user is following
                Idfollowing = user.FollowTables.Contains()
                Idnotfollowing = 
                 });
        return Json(ret, JsonRequestBehavior.AllowGet);
  }

FollowTable的结构如下:

ID  UserId  FollowId
1    4        11
2    4        12
2    4        13

这里,当前登录用户的id是4,他跟随11,12,13所以我想只返回11,12和13给Idfollowing并在Idnotfollowing中剩下剩余的id。如何完成它。

好吧,我认为对于列表或数组,我不会得到理想的结果。所以,我想在这里添加一些东西。 好吧,对于每个UserName,id也会传递给视图页面。所以,我把它们分成两部分。现在,如何为这些ID分配值。

Comapre User.Id与当前登录用户的关注表的followId列。如果找到匹配.ie,如果id匹配或找到,则分配该user.Id为Idfollowing并且null为Idnotfollowing,反之亦然。< /强> 我必须根据返回的这些ID生成关注取消关注按钮。

 public JsonResult GetUsers()
    {
        int currentUserId = this.User.Identity.GetUserId<int>();
        var ret = (from user in db.Users.ToList()
                   let Id = user.FollowTables.Where(x => x.UserId == currentUserId).Select(f => f.FollowId).ToList()
                   let Idnot = (from user2 in db.Users
                                where !Id.Contains(user2.Id)
                                select user2.Id).ToList()
                   select new
                   {
                       UserName = user.UserName,
                       Id = Id,
                       //Id = user.FollowTables.Where(x => x.UserId == currentUserId)
                       //       .Select(x => x.FollowId).Single(),
                       Idnot = Idnot, 

3 个答案:

答案 0 :(得分:1)

您似乎拥有从UserFollowTable的标准一对多关系。此数据模型强制user.FollowTables 包含关注者。您将无法直接从Idnotfollowing媒体资源填写FollowTables

这样的事情可能有用:

var query = (
    from user in db.Users // note: removed ToList() here 
                          // to avoid premature query materialization
    where //TODO ADD WHERE CLAUSE HERE ?
    let followIds = user.FollowTables.Select(f => f.FollowId)
    let notFollowIds = (from user2 in db.Users
                        where !followIds.Contains(user2.Id)
                        select user2.Id)
    select new 
    {
        UserName = user.UserName,
        Idfollowing = followIds.ToArray(),
        Idnotfollowing = notFollowIds.ToArray()
    })
     // TODO add paging? .Skip(offset).Take(pageSize)
     .ToList();

验证此查询生成的SQL并确保它执行正常但是......

另请注意,我从.ToList()中删除了db.Users.ToList(),以避免过早的查询实现。无论如何从一个无约束的表中提取所有数据通常是一个坏主意,你通常会想要一个

答案 1 :(得分:0)

var ret = (from user in db.Users.ToList()
              select new 
               {
                UserName = user.UserName,
                Idfollowing = user.FollowTables.Select(x=> x.Id)
                Idnotfollowing = db.FollowTables.Where(x=> !user.FollowTables.Select(x=> x.Id).Contains(x.Id)).Select(x=> x.Id)
             });

它很丑但会起作用,必须有另一种更好的方法。

答案 2 :(得分:0)

您只需使用Where方法过滤表格,然后使用Select投放FollowiId: -

var ret = (from user in db.Users.ToList()
           select new 
             {
                UserName = user.UserName,
                Idfollowing = user.FollowTables.Where(x => x.UserId == user.Id)
                                  .Select(x => x.FollowId).ToArray(),
                Idnotfollowing = user.FollowTables.Where(x => x.UserId != user.Id)
                                  .Select(x => x.FollowId).ToArray()
            });

假设Idfollowing&amp; Idnotfollowing是整数数组(如果FollowId是整数),否则如果它是一个列表,你可以用ToList替换它。