如何根据Id向LINQ查询添加条件

本文关键字:查询 添加 条件 LINQ 何根 Id | 更新日期: 2023-09-27 18:28:57

这是我的操作方法,它获取所有具有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

在这里,当前loggedin用户的id是4,他正在关注11、12、13,所以我只想将11、12和13返回到Idfollowing,其余的id在Idnotfollowing中。如何完成。

好吧,我认为用列表或数组,我不会得到想要的结果。所以,我想在这里添加一些内容。好吧,对于每个UserName,也会向查看页面传递一个id。所以,我把它们一分为二。现在,如何为这些id赋值。

在用户的followId列中使用当前日志比较User.Id。如果找到匹配项,即id匹配或找到,则分配该用户。Id为Idfollowing,null为Idnotfollowing;反之亦然我必须根据这些返回的id生成follow unfollow按钮。

 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, 

如何根据Id向LINQ查询添加条件

UserFollowTable似乎有一个标准的一对多关系。此数据模型强制user.FollowTables包含跟随者。您将无法直接从FollowTables属性中填写Idnotfollowing

像这样的东西可能会起作用:

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,并确保其执行正常。。。

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

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)
             });

这很难看,但会起作用,必须有另一种更好的方法。

您可以简单地使用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&如果是整数,则Idnotfollowing是数组(如果FollowId是整数),否则如果是列表,则可以用ToList替换它。