Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ To SQL: Filtering a query across many tables
    primarykey
    data
    text
    <p>I've inherited this project from the previous guy, and I'm presently in the process of switching over a number of assembled-from-string SQL queries into LINQ to SQL, which I'm told is what makes life in .NET worth living. However, I'm running into problems filtering a Many-to-Many relationship. </p> <p>The system is a Project Management intranet affair. The primary object/table of note is <em>Project</em>. <em>Project</em> has many <em>Markets</em>, which uses an intermediary table <em>linkProjectMarkets</em> to link them. There's plenty of other stuff going on, but I can take care of that if I can understand this problem, it's all similar.</p> <p>In short, I can't for the life of me take a list of Market IDs provided by a set of checkboxes, and filter the list of projects down to those which have a relation to the markets whose ids are provided.</p> <p>The gist of the schema is:</p> <p><strong>Project</strong>: int id, ...(a bunch of metadata)<br> <strong>linkProjectMarkets</strong>: int projectId, int marketId<br> <strong>Market</strong>: int id, string summary</p> <p>All of this, any many, many more, is defined in a dbml file and accompanying generated code. To facilitate access between <em>project</em> and <em>market</em>, <em>project</em> has the following addon method:</p> <pre><code>public partial class project { public EntitySet&lt;market&gt; markets { get { return (EntitySet&lt;market&gt;)from lpm in this._linkProjectMarkets select lpm.market; } set { var set = (EntitySet&lt;market&gt;)from lpm in this._linkProjectMarkets select lpm.market; set.Assign(value); } } } </code></pre> <p>Here's the most recent bit of code I'm attempting to use to filter the set of all projects:</p> <pre><code>var projects = from p in db.projects select p; //MarketFilterList is an IEnumerable of market ids created from the checked checkboxes. if(MarketFilterList.Count() &gt; 0){ foreach (int mid in MarketFilterList){ projects = projects.Where(p =&gt; p.markets.Select(m =&gt; m.id).Contains(mid)); /* Causes "Member access 'Int32 id' of 'PMIS2.market' not legal on type 'System.Data.Linq.EntitySet`1[PMIS2.market]." */ } } </code></pre> <p>There are a number of other filters to apply, which is why I'm doing it in steps instead of one LINQ statement.</p> <p>I checked out the designer code; market in linkProjectMarkets is public, and id is public in market. It's my first real C#/.NET experience, so maybe I just don't understand the error message. Apologies if I've given too much, not enough, or not the right information. Please help!</p> <p>P.S. I already tried LINQ to Entities, and I would have to have a really good reason to try it again.</p> <h2>Edit</h2> <p>Here's something else I tried:</p> <pre><code>projects = projects.Where( p =&gt; MarketFilterList.Intersect(from m in p.markets select m.id).Count() &gt; 0 ); </code></pre> <p>That one told me:</p> <blockquote> <p>Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator.</p> </blockquote> <h2>Edit again</h2> <p>Here's the solution based on JTew's answer. If anyone has the real reason why project can't access market directly I'd love to hear it.</p> <pre><code>projects = projects.Where(p =&gt; (from lpm in db.linkProjectMarkets where MarketFilterList.Contains(lpm.marketId) select lpm.project).Contains(p)); </code></pre>
    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.
 

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