简体   繁体   English

查询语法有效,但lambda语法不适用于linq select语句

[英]Query syntax works but lambda syntax doesn't for linq select statement

This code doesn't work: 该代码不起作用:

return this.Context.StockTakeFacts.Select(stf => ((stf.StockTakeId == stocktakeid) && (stf.FactKindId == ((int)kind)))).ToList<IStockTakeFact>();

This statement does: 该语句可以:

        var f = from stf in this.Context.StockTakeFacts
                where (stf.StockTakeId == stocktakeid) && (stf.FactKindId == ((int)kind))
                select stf;
        return f.ToList<IStockTakeFact>();

What is the difference?? 有什么区别?? The first complains that IQueryable does not have a toList method so I gather I've written the first statement incorrectly. 第一个抱怨IQueryable没有toList方法,所以我收集到我写错了第一条语句。

You need to use a Where call in order to filter elements (not Select ) 您需要使用Where调用才能过滤元素(而非Select

return this.Context.StockTakeFacts
  .Where(stf => ((stf.StockTakeId == stocktakeid) && (stf.FactKindId == ((int)kind))))
  .ToList<IStockTakeFact>();

When using explicit LINQ API queries the select item is implicit. 使用显式LINQ API查询时, select item是隐式的。 It can be made explicit with a call to Select but it's not necessary (unless you map the values in some way) 可以通过调用Select使其明确,但这不是必需的(除非您以某种方式映射值)

You have to use Where() in order to be able filtering by the predicate: 您必须使用Where()才能通过谓词进行过滤:

return this.Context.StockTakeFacts
                    .Where(stf => stf.StockTakeId == stocktakeid 
                                  && stf.FactKindId == (int)kind)
                    .ToList<IStockTakeFact>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM