![](/img/trans.png)
[英]How to compare two byte arrays with greater than or less than operator value in C# or Linq?
[英]LINQ include “&&” operator if value is greater than zero
當leaId
和seaId
大於零時,以下方法返回數據。
如果僅輸入leaId
應如何更改查詢以返回數據?
由於&&
運算符,因此沒有返回任何內容,因為seaId
是可選的並且等於零。
public IQueryable<Stand> GetStand(int leaId, int seaId = 0)
{
return from c in db.Stands where c.LeaID == leaId && c.SeaID == seaId
select new Stand
{
//something
}
}
將查詢分為兩部分:
IQueryable<Stand> query=db.Stands.Where(c= c.LeaID == leaId);
if(seaId>0)
{
query=query.Where(c= c.SeaID == seaId);
}
return query.Select(e=>...);
另一個重要的事情是您不應該使用實體類型( Stand
)來投影查詢。 在EF中,您可以通過匿名類型或使用DTO進行投影。 如果您要使用自定義類進行投影,請忽略最后一條注釋。
您可以像這樣修改您的方法
public IQueryable<Stand> GetStand(int leaId, int seaId = 0)
{
return from c in db.Stands where (c.LeaID == leaId) ||(seaId>0 && c.SeaID == seaId)
select new Stand
{
//something
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.