Searching for multiple strings using single database query with entity framework and LINQ

c# entity-framework entity-framework-6 linq linq-to-sql

Question

Let's say I have a database table with two columns called aTable.

id: int

string name

My goal is to extract entries where aTable.name is a list of strings (stringsToSearchFor).

What I'm doing: At the moment, I'm using the following strategy.

var result=new List<aTable>; 
foreach (var aString in stringsToSearchFor)
    {
        var r = Context.Set<aTable>()
                .Any(s => s.name.Contains(searchString))
                .ToList();
        res.AddRange(r);
    }
return result.Distinct();

I attempted to modify the code to make it more efficient by removing the foreach, making it:

return Context.Set<aTable>()
                    .Any(s => stringsToSearchFor.Contains(s.name))
                    .Distinct()
                    .ToList();

This didn't, however, have the same outcomes as the preceding assertion. I agree that the first claim is true.

How can I search over many strings without having to create N database queries, as in the second approach?

1
0
8/20/2018 9:37:58 AM

Popular Answer

ZZZ_tmp
0
8/20/2018 10:01:26 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow