Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with Group By in LinQ Query
    primarykey
    data
    text
    <p>I want to make a LinQ query against an array with and return multiple columns. I can't seem to get it working correctly, and I've tried various things.</p> <p>First off, the array contains a type and a version from a table such as:</p> <pre><code>TypeID-------VersionID-------Text 1 1 "Version 1.1" 2 1 "Version 2.1" 3 1 "Version 3.1" 3 2 "Version 3.2" </code></pre> <p>This is a SQL table, and I could easily get the latest version with this query:</p> <pre><code>SELECT V.* FROM Table V WHERE V.VersionID IN (SELECT MAX(VersionID) GROUP BY TypeID) </code></pre> <p>However, I want to do this in LinQ and it's killing me. I can't figure it out. I want all the values. Therefore:</p> <pre><code>public MyStruct { public int TypeID, public int VersionID, public string Text } MyStruct[] array = new MyStruct[4]; array[0].TypeID = 1; array[0].VersionID = 1; array[0].Text = "Version 1.1"; array[1].TypeID = 2; array[1].VersionID = 1; array[1].Text = "Version 2.1"; array[2].TypeID = 3; array[2].VersionID = 1; array[2].Text = "Version 3.1"; array[3].TypeID = 3; array[3].VersionID = 2; array[3].Text = "Version 3.2"; </code></pre> <p>Now, I want a resultant array that only has three elements containing Version 1.1, Version 2.1, and Version 3.2. Since Version 3.1 is an "older version", I want to ignore it.</p> <p>My LinQ is like this:</p> <pre><code>var myArrayQuery = from arr in array group arr by arr.TypeID into arrGroup select new { TypeID = arrGroup.Key, VersionID = (int)arrGroup.Max(mx=&gt;mx.VersionID) // -- GET THE TEXT HERE --- }; </code></pre> <p>I can't figure out how to get the text!</p> <p>I have tried:</p> <pre><code>var myArrayQuery = from arr in array group arr by new{ arr.TypeID, arr.VersionID, arr.Text} into arrGroup select new { TypeID = arrGroup.TypeID, VersionID = (int)arrGroup.Max(mx=&gt;mx.VersionID), Text = arrGroup.Text }; </code></pre> <p>But then, it returns <em>all four</em> of my array elements. Therefore, no good. I don't get the MAX like I want.</p> <p>Please help!</p> <p>Thanks.</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