简体   繁体   English

LINQ多个from子句为空值

[英]LINQ multiple from clauses null value

Hi I'm fairly new to this, hope you can help 嗨,我对此并不陌生,希望您能为您提供帮助

I found the following SO article which shows how to filter a LINQ query LINQ to SQL selecting all records which have any word in the string split 我找到了下面的SO文章,该文章显示了如何将LINQ查询LINQ筛选为SQL,以选择所有在字符串拆分中具有任何单词的记录

What I can't figure out is how to return all results if the filter array is empty. 我不知道的是,如果过滤器数组为空,如何返回所有结果。 I've tried: 我试过了:

  ... = (from string A in lstStrings
                           from string B in strArray
                           where B == null || A.Contains(B) ..

and tried including empty string: 并尝试包括空字符串:

  ... = (from string A in lstStrings
                           from string B in strArray
                           where B == null || B == "" || A.Contains(B) ..

None of which work 都不起作用

Edit 1: 编辑1:

I was using the answer from the article here is my actual code 我正在使用文章的答案,这是我的实际代码

string[] filterlist = Regex
                   .Matches(sfilter, @"(?<match>\w+)|\""(?<match>[\w\s]*)""")
                   .Cast<Match>()
                   .Select(m => m.Groups["match"].Value)
                   .ToArray();

var stk = await (from c in ctx.INVENTORY
                            from f in filterlist
                         where f == null || c.DESC.ToUpper().Contains(f.ToUpper())
                         select c).ToListAsync<INVENTORY>();'

ctx is my DBEntityContext. ctx是我的DBEntityContext。

Edit 2: 编辑2:

I should mention my code does work almost as expected, if I type a search string it finds what I'm looking for, but if the string is empty I get nothing. 我应该提一下,我的代码确实可以按预期工作,如果我键入一个搜索字符串,它会找到我想要的东西,但是如果该字符串为空,我什么也没得到。

Only apply a more restrictive WHERE clause if the filterlist isn't empty like so: 如果过滤器列表不为空,则仅应用更具限制性的WHERE子句,如下所示:

var stk = lstStrings;
if (filterlist!=null && filterlist.Any())
{
    stk = stk.Where(a=>filterlist.Any(b=>a.Contains(b)));
}

Using your code: 使用您的代码:

string[] filterlist = Regex
                   .Matches(sfilter, @"(?<match>\w+)|\""(?<match>[\w\s]*)""")
                   .Cast<Match>()
                   .Select(m => m.Groups["match"].Value)
                   .ToArray();

var query= ctx.INVENTORY.AsQueryable();
if (filterList!=null && filterList.Any())
{
  query=query.Where(i=>filterList.Any(fl=>i.Contains(fl));
}
var stk = await query.ToListAsync<INVENTORY>();

You can do it using LINQ extensions like this: 您可以使用LINQ扩展来做到这一点:

var result = lstStrings
            .Where(a => strArray == null || !strArray.Any() || strArray.Any(a.Contains))
            .Distinct();

You can alternatively of course just use an if statement to short circuit the whole ordeal, like so: 当然,您也可以使用if语句来缩短整个测试过程,如下所示:

if (strArray == null || !strArray.Any())
{
   ... = lstStrings;
}
else 
{
   ... = (from string A in lstStrings
               from string B in strArray
               where B == null || B == "" || A.Contains(B) ..
}
var stk = ctx.INVENTORY.AsQueryable();
if (filterlist != null && filterlist.Any()){
    filterlist = filterlist.Where(w => w != null).ToArray();
    stk = stk.Where(w => filterlist.Any(a => w.DESC.ToUpper().Contains(a.ToUpper())));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM