本文介绍了LINQ查询所有SearchVal是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个winForms应用,其中用户汽车搜索车辆".如果给定一个值,我的查询效果很好,但是如果将其保留为空白,它将什么也不会检索("cos基本上正在执行.Contains(")).
在普通的SQL查询中,您将使用以下内容:
选择*从需要"%的车辆+ SearchVal +"%"

如果Search val为空,查询将仅返回所有内容.

但是在LINQ中,我必须这样做:
来自v in ent.Vehicles
其中v.Make.Contains(SearchVal)
选择
{
...
}

现在,如果Searchval为空,则不返回任何内容(使sens cos不查找任何内容).如何在这里获得理想的结果?

任何帮助将不胜感激.
感谢

We have a winForms app in which a user car search for Vehicles. My query works fine if a value is given, but if you leave it blank it retrieves nothing (''cos is basically executing .Contains("")).

With a normal SQL query you would use something like this:
"SELECT * FROM Vehicles WHERE Make LIKE ''%" + SearchVal + "%''"

If Search val is empty, the query will simply return everything.

But in LINQ I have to do this:
from v in ent.Vehicles
where v.Make.Contains(SearchVal)
Select
{
...
}

Now if Searchval is empty it return nothing (makes sens cos it''s looking for nothing). How can I get the desired result here?

Any help would be appreciated.
Thanks

推荐答案

from v in ent.Vehicles
where v.Make.Contains(SearchVal) || String.IsNullOrEmpty(SearchVal)
Select
{
...
}


这篇关于LINQ查询所有SearchVal是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 11:20