Reputation: 99
I have
string[] centersArr = new string[] {253, 6789, 9080};
I want to query database where center number start with any of the items of above centers array and the 8th character of center should be '1'.
var result = disctrict.Where(x => centersArr.Contains(x.Center) && x.Center[7] == '1').ToList();
My database has center numbers like 2533455, 2537890, 25312678, 678912 and so on..
Above query is returning null result. Please help me how to write this query.
Upvotes: 1
Views: 2434
Reputation: 99
i solved this:
.Where(x => centersArr.Any(c => x.Center.StartsWith(c)) && ....)
Upvotes: 1
Reputation: 9500
centersArr.Contains(x.Center)
is not going to work.
The items in centersArr
genuinely cannot contain any of the example database values that you give. And I suspect that's true for all of your database values.
In essence you are asking for a situation where the following is true (the following is super pseudo pseudocode)
ListOfCenters('A','B','C').Contains(ListOfLongerItemsFromDatabase('WORDA', 'BWORD', 'WORDC').
That's exactly backward. WORDA or BWORD or WORDC can and do contain A,B, or C. But not the reverse.
Does that help clarify?
This, however, should work:
var result = disctrict.Where(x => (x.Center.Contains("253") || x.Center.Contains("6789") || x.Center.Contains("9080")) && x.Center[7] == '1').ToList();
unless Linq to SQL cannot translate that into a SQL expression . . .. If it doesn't work, at least it shows conceptually what you really want to be doing instead.
Upvotes: 0