I am stuck on a LINQ query where I have being trying to return a list records from an SQL Table
using EntityFramework 6
, instead of getting that list, I keep end up getting an IEnumerable<string[]>
.
This is what I have made to get IEnumerable<string[]>
. I have an column
in the table
that needs to be split and then I test to see if a list
contains those strings : The value in the table column columnOne
can be something like this "a-b"
or "b-c"
etc, hence the need to use this s.columnOne.Split('-')
list<string> checkList = new list<string>();
checkList.add("a")
checkList.add("b")
checkList.add("c")
List<Foo> fooList = dbContext.Foos.ToList();
IEnumerable<string[]> items = fooList.Select(s => s.columnOne.Split('-'));
var result = items.SelectMany(x => x)
.Where(s => checkList.Contains(s)).ToList();
The above works as it should, but in the end it returns a list of string
, which is not what I need.
I tried this below:
List<Foo> fooList = dbContext.Foos.ToList();
var test = fooList.Where(s => s.columnOne.Split('-'));
And this is where I run into the error and can go no further as I am ultimately trying to return a list of <Foo>
, not <string>
Now I know that the Where
clause needs to return a bool
value such as fooList.Where(s => s.columnOne == "someString");
, but I am at a loss as to how to go about structuring the LINQ query to get the results I am trying to get.
Any shove in the right direction would be great.
The where clause needs to contain the check against the checkList
var test = fooList.Where(foo => foo.columnOne.Split('-').Any(str => checkList.Contains(str)));
dbContext.Foos
.ToList()
.Where(foo => foo.columnOne.Split('-')
.Any(x => checkList.Contains(x))
I hope you don't ever have very many Foo
s because that ToList
function will read them all into memory. You'll need that, though, because I don't think Linq can understand how to translate that where
statement. There are a few ways to get around that, if it's a problem to you.
Also, consider using a Set<string>
instead of a List<string>
for your checklist
variable, as it'll perform better for membership checks like this one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With