I have a fairly complicated linq query that returns an item and a string[] of one column in the item's many-to-many relationship.
So I return an anonymous type sort of like this:
var q = from k in db.Items
join m in db.ManyRel on k.Id equals m.ItemID
select new
{
k,
list = (from t in m
where t.ItemID == k.ID
select t.Name)
}
So the anonymous type is {IQueryable<k>, IQueryable<string>}
.
What I then want to do is search through the list for intersections from string[] input
.
q.Select(x => new
{
x.k,
PartialMatches = ?????,
ExactMatches = x.tags.Intersect(input).Count()
});
I currently have it working with .Intersect, but .Intersect() only matches against the entire element. I would like to somehow use .Contains() within .Intersect() to count the number of partial matches.
I would prefer to do this without an extension method if possible.
Edit: @Jon & Timwi
I tried both of your versions, they were pretty much the same, and they find if ANY item in input partially exists within x.tags. I'd like to be able to count how many items in input partially exist within x.tags, not just whether AN item exists. Thanks for the responses.
I think I have a better handle on the AsEnumerable() stuff than I did a few nights ago.
Also, for the record, because you where both curious, the Intersect() method does not translate to Linq to SQL and neither do the any methods you each proposed. They all work well and fast enough with AsEnumberable() however.