pra*_*ech 2 linq asp.net linq-to-entities
我对LINQ很陌生。我有一个下面的LINQ查询
var entities = context.MessagingTemplateEntities
.Where(m =>
m.PartyId == partyId &&
m.MessageTemplateTypeId == messagingTemplateTypeId &&
m.ProductTypePartyId == productTypePartyId);
Run Code Online (Sandbox Code Playgroud)
在此查询中,如果productTypePartyId为0,那么我不想在&&条件中包括它。那么如何根据值排除参数?
不知道为什么人们总是强迫LINQ成为一名班轮。
var entities = context.MessagingTemplateEntities
.Where(m =>
m.PartyId == partyId &&
m.MessageTemplateTypeId == messagingTemplateTypeId);
if(productTypePartyId != 0)
entities = entites.Where(m.ProductTypePartyId == productTypePartyId);
Run Code Online (Sandbox Code Playgroud)
一目了然地决定自己更容易阅读。