在编写具有多个"和"条件的LINQ查询时,我应该编写where包含&&多个where子句的单个子句,每个条件一个吗?
static void Main(string[] args)
{
var ints = new List<int>(Enumerable.Range(-10, 20));
var positiveEvensA = from i in ints
where (i > 0) && ((i % 2) == 0)
select i;
var positiveEvensB = from i in ints
where i > 0
where (i % 2) == 0
select i;
System.Diagnostics.Debug.Assert(positiveEvensA.Count() ==
positiveEvensB.Count());
}
Run Code Online (Sandbox Code Playgroud)
是否有任何差别比之间的个人喜好或编码风格(排长队,可读性等)等positiveEvensA和positiveEvensB?
想到的一个可能的区别是,不同的LINQ提供者可能能够更好地处理多个wheres而不是更复杂的表达; 这是真的?
写作是否存在(逻辑/性能)差异:
ATable.Where(x=> condition1 && condition2 && condition3)
要么
ATable.Where(x=>condition1).Where(x=>condition2).Where(x=>condition3)
我一直在使用前者,但意识到使用后者,我可以阅读和复制部分查询,以便更容易在其他地方使用.有什么想法吗?
我有以下课程:
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; }
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以像这样使用Linq查询:
.Where(a => a.SubjectId == subjectId)
Run Code Online (Sandbox Code Playgroud)
但是,我怎么能这样做,所以还有另一个条件
.Where(a => a.ContentTypeId == contentTypId)
Run Code Online (Sandbox Code Playgroud)
有没有办法将这些加入到哪一个或者它们应该保持为两个?