Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've tried to rewrite your code, but it isn't an easy task and I'm not sure if it works. I have set the binding of each column to a sequential index of an array <code>Keywords["+i+"]"</code> instead of <code>documents_keywords.FirstOrDefault(</code> and have changed a structure of the ItemSource. </p> <p>I have no opportunity to check this code myself, so if you have any problems - write them in the comment section.</p> <pre><code> DataGrid dataGrid = new DataGrid(); dataGrid.Columns.Add(new DataGridTextColumn { Header = "Title", Binding = new Binding("title") }); //all possible keywords var items = db.keywords.Select(k =&gt; new {Id = k.keyword_id, Name = k.name}).ToArray(); //selected keywords ordered by id var selected = (from item in items where keywordsListBox.SelectedItems.Contains(item.Name) orderby item.Id select item) .ToArray(); //create columns and bind them for(int i = 0; i &lt; selected.Length; i++) { dataGrid.Columns.Add(new DataGridTextColumn { Header = selected[i].Name, Binding = new Binding("Keywords["+i+"]") }); } var documents = (from d in db.documents select new{ d.title, //All related keywords Keywords = d.documents_keywords.Select(dk =&gt; new { Id = dk.keywoard_id, Value = dk.value}) .ToList()}) .AsEnumerable() .Select(doc =&gt; new { title = doc.title, //Only selected keywords with default null values Keywords = (from si in selected join k in doc.Keywords on si.Id equals k.Id into j from ji in j.DefaultIfEmpty(new { Id = si.Id, Value = null}) orderby ji.Id select ji.Value) .ToArray() }); dataGrid.ItemsSource = documents.ToList(); </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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