简体   繁体   中英

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

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:

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.

Edit 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:

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:

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 (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())));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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