Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ many-to-many in WPF databinding path
    text
    copied!<p>in my c# WPF app i add columns to a DataGrid at runtime, and populate from my SQL db via LINQ. this is working fine until i try to add data from my many-to-many table </p> <p>here's a simplified version of the 3 relevant tables my db:</p> <pre><code>documents: document_id, title documents_keywords: document_id, keyword_id, value keywords: keyword_id, name </code></pre> <p>what i want in my DataGrid is a column for document.title, plus a column for each record in document.documents_keywords based on user selection. here's my code:</p> <pre><code> DataGrid dataGrid = new DataGrid(); dataGrid.Columns.Add(new DataGridTextColumn { Header = "Title", Binding = new Binding("title") }); foreach (string keywordName in keywordsListBox.SelectedItems) { dataGrid.Columns.Add(new DataGridTextColumn { Header = keywordName, Binding = new Binding("documents_keywords.FirstOrDefault(kw =&gt; kw.keyword.name.Equals(\""+keywordName+"\")).value") }); } dataGrid.ItemsSource = from d in db.documents select d; </code></pre> <p>i'm getting the keyword headers, but the cells are all blank. the binding fails at FirstOrDefault:</p> <pre><code>System.Windows.Data Error: 40 : BindingExpression path error: 'FirstOrDefault(d_k =&gt; d_k.keyword.name.Equals("Order#"))' property not found on 'object' ''EntityCollection`1' (HashCode=7935090)'. BindingExpression:Path=documents_keywords.FirstOrDefault(d_k =&gt; d_k.keyword.name.Equals("Order#")).value; DataItem='document' (HashCode=5781744); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') </code></pre> <p>what is the best way to accomplish this? forgive me as this is my first jaunt into WPF and LINQ. </p>
 

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