是否可以在LINQ查询中将两个Where子句连接在一起? [英] Can or should I join two Where clauses together in a LINQ Query?
本文介绍了是否可以在LINQ查询中将两个Where子句连接在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下课程:
public partial class Content
{
public int ContentId { get; set; }
public int ContentTypeId { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public int SubjectId { get; set; }
public virtual Subject Subject { get; set; }
}
我知道我可以使用这样的Linq查询:
I understand I can use a Linq query like this:
.Where(a => a.SubjectId == subjectId)
但是我该怎么做,所以还有另一种情况
However how can I make it so there is another condition
.Where(a => a.ContentTypeId == contentTypId)
有没有一种方法可以将它们合并为一个,还是应该保留为两个?
is there a way I can join these into one where or should they remain as two?
推荐答案
仅使用一个包含每个条件的Where
子句:
Using only one Where
clause containing every condition:
.Where(a => a.SubjectId == subjectId && a.ContentTypeId == contentTypId)
或两个Where
子句,每个子句处理一个条件:
Or two Where
clauses, dealing with one condition each:
.Where(a => a.SubjectId == subjectId)
.Where(a => a.ContentTypeId == contentTypId)
是等效的,因为LINQ查询的执行被推迟到对结果的调用之前.
is equivalent, as the LINQ query execution is deferred until the call to the result.
这篇关于是否可以在LINQ查询中将两个Where子句连接在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文