C# Lambda 表达式:包含列表的 Where 子句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15810265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:00:56  来源:igfitidea点击:

Lambda expression: Where clause for Include list

c#linqlambda

提问by Tikkes

So I got something like this:

所以我得到了这样的东西:

var myObj = db.SomeObject
              .Include("Tasks")
              .SingleOrDefault(x => x.Id == someObjectId);

if (myObj != null)
{
    myObj.Tasks = myObj.Tasks.OrderBy(x => x.Number).ToList();
}

Here I want to be able to put a condition (where) on my Include, for instance: .where task.IsDeleted == false

在这里,我希望能够where在我的 Include 上放置一个条件 ( ),例如: .where task.IsDeleted == false

Thusfar I have not found a solution.

到目前为止,我还没有找到解决方案。

I am aware of the fact that I could use the wheretogether with where I order the tasks but this however, does not run on the database but in stead uses memory. I want it to run on the database.

我知道我可以将where与我订购任务的位置一起使用,但是这不会在数据库上运行,而是使用内存。我希望它在数据库上运行。

Does anyone here know how I can do this? If so, is there also a way to put the order bycondition to the included list of tasks?

这里有人知道我该怎么做吗?如果是这样,是否还有一种方法可以将order by条件放入包含的任务列表中?

采纳答案by Skyp

Something like this, returs you your original object with its child collection filtered and sorted.

像这样,通过过滤和排序的子集合返回您的原始对象。

SomeObject a = db.SomeObjects.Where(x => x.Id == someobjectid)
                      .Select(
                          x =>
                          new
                              {
                                  someObject = x,
                                  task = x.Tasks.Where(task => task.IsDeleted == false)
                                                .OrderBy(task => whatever)
                              })
                      .Select(x => x.someObject).Single();

It's actually loosing the collection of activities in the last select so you can do this :

它实际上丢失了最后一次选择中的活动集合,因此您可以这样做:

SomeObject a = db.SomeObjects.Where(x => x.Id == someobjectid)
                      .Select(
                          x =>
                          new
                              {
                                  someObject = x,
                                  task = x.Tasks.Where(task => task.IsDeleted == false)
                                                .OrderBy(task => whatever)
                              });
 return a.FirstOrDefault().someObject;

回答by Daniel Hilgarth

The simple answer is: You can't do that.

简单的答案是:你不能那样做。

Why? Because you query SomeObject.
Each returned SomeObjectcontains all referenced data, because if it wouldn't it wouldn't represent the actual object in the database.

为什么?因为你查询SomeObject.
每个返回的SomeObject数据都包含所有引用的数据,因为如果不是,它就不会代表数据库中的实际对象。

回答by lahsrah

What about getting them separately:

分别获取它们怎么样:

var myObj = db.SomeObject
              .SingleOrDefault(x => x.Id == someObjectId);


var tasks = db.SomeObject
              .Where(x => x.Id == someObjectId)
              .SelectMany(x => x.Tasks)
              .Where(x => !x.Deleted);

回答by Nicholas Butler

To do this is EF, you need to specify a projection with a Selectclause.

要做到这一点是 EF,您需要指定一个带有Select子句的投影。

Something like this will get just the data you want from the db:

这样的事情只会从数据库中获取您想要的数据:

var anonymous = db.SomeObject.Where( x => x.Id == someObjectId )
  .Select( x => new
    {
      SomeObject = x,
      Tasks = x.Tasks
        .Where( o => !o.IsDeleted )
        .OrderBy( o => ... )
    }
  )
  .SingleOrDefault()
;

You will end up with an instance of the anonymous type , but you can easily fix that up on the client:

你最终会得到一个匿名类型的实例,但你可以很容易地在客户端上修复它:

MyObject myObject = anonymous.SomeObject;
myObject.Tasks = anonymous.Tasks;