Reputation: 499
I have a list of string like this
List<string> list1 = new List<string>();
For example my list is
{one , two , three , four , five}
In database I have a string
like this
"five-six-seven-eight"
How I can use Linq to SQL to search between my string in database and my list. For example my query search should be true because "five" is common on both. My query is this:
var q = from p in m.ViewAbuseReports where
(list1.Contains(p.Abuse.ToString().Split('-')) || list1.Count == 0)
select new
{
Col2 = p.FirstName + " " + p.LastName,
Col3 = p.DateTakhalof,
Col4 = p.DateReturn
};
Upvotes: 2
Views: 781
Reputation: 14498
var q = from p in m.ViewAbuseReports where
(list1.Any(l => p.Abuse.ToString().Contains(l)) || list1.Count == 0)
select new
{
Col2 = p.FirstName + " " + p.LastName,
Col3 = p.DateTakhalof,
Col4 = p.DateReturn
};
If any of the strings in list1
is in p.Abuse
this will be true. This way you don't have to take care of splitting the string (which would be a hard thing to do in database).
Upvotes: 2
Reputation: 30813
You may consider using LINQ Intersect
.
var q = from p in m.ViewAbuseReports
let ps = p.Abuse.ToString().Split('-')
let i = list1.Intersect(ps).ToList() //here you get if there is any intersect (common elements) between the two `IEnumerable`
where (i.Count > 0 || list1.Count == 0)
select new
{
Col2 = p.FirstName + " " + p.LastName,
Col3 = p.DateTakhalof,
Col4 = p.DateReturn
};
Upvotes: 3