Note that there are some explanatory texts on larger screens.

plurals
  1. POPerforming a Contains within an Intersect
    primarykey
    data
    text
    <p>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.</p> <p>So I return an anonymous type sort of like this:</p> <pre><code>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) } </code></pre> <p>So the anonymous type is <code>{IQueryable&lt;k&gt;, IQueryable&lt;string&gt;}</code>.</p> <p>What I then want to do is search through the list for intersections from <code>string[] input</code>.</p> <pre><code>q.Select(x =&gt; new { x.k, PartialMatches = ?????, ExactMatches = x.tags.Intersect(input).Count() }); </code></pre> <p>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.</p> <p>I would prefer to do this without an extension method if possible.</p> <p>Edit: @Jon &amp; Timwi</p> <p>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. </p> <p>I think I have a better handle on the AsEnumerable() stuff than I did a few nights ago.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload