[英]LINQ query with multiple conditional where clauses
I can't seem to figure out how to make a LINQ query with where clauses based on multiple nullable variables work. 我似乎无法弄清楚如何使用基于多个可空变量的where子句进行LINQ查询。 I've tried the "if" methods I've seen on this site, and I get the error:
我尝试了在此站点上看到的“ if”方法,但出现错误:
The name I doesn't exist in the current context.
我的名字在当前上下文中不存在。
The "Point" fields are formatted as "POINT(num1, num2)" and work fine in a SQL query. “点”字段的格式设置为“ POINT(num1,num2)”,并且在SQL查询中可以正常工作。
query = from i in _db.ILV
join p in _db.PC on
i.PostCode equals p.PostCode
select i;
if (!string.IsNullOrEmpty(key))
query = query.Where(i = i.Description.Contains(key));
if(!string.IsNullOrEmpty(rng))
query = query.Where(i => STGeomFromText(i.Point).STDistance(q.Point) <= rng);
if (!string.IsNullOrEmpty(cat))
query = query.Where(i = i.CategoryID.ToInt(cat));
if(!string.IsNullOrEmpty(sub))
query = query.Where(i = i.SubCategoryID.ToInt(sub));
return query;
That would work if you used i => instead of i = . 如果您使用i =>而不是i =,那将起作用。 You can put it all in one block however, like follows:
但是,您可以将所有内容放在一起,如下所示:
query = from i in _db.ILV
join p in _db.PC on
i.PostCode equals p.PostCode
where (!string.IsNullOrEmpty(key)) ? i.Description.Contains (key) : true &&
(!string.IsNullOrEmpty(rng)) ? // rest of your where clauses follow the same pattern
select i;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.