c# - Issue with Group By in LinQ Query -
i want make linq query against array , return multiple columns. can't seem working correctly, , i've tried various things.
first off, array contains type , version table such as:
typeid-------versionid-------text 1 1 "version 1.1" 2 1 "version 2.1" 3 1 "version 3.1" 3 2 "version 3.2"
this sql table, , latest version query:
select v.* table v v.versionid in (select max(versionid) group typeid)
however, want in linq , it's killing me. can't figure out. want values. therefore:
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";
now, want resultant array has 3 elements containing version 1.1, version 2.1, , version 3.2. since version 3.1 "older version", want ignore it.
my linq this:
var myarrayquery = arr in array group arr arr.typeid arrgroup select new { typeid = arrgroup.key, versionid = (int)arrgroup.max(mx=>mx.versionid) // -- text here --- };
i can't figure out how text!
i have tried:
var myarrayquery = arr in array group arr new{ arr.typeid, arr.versionid, arr.text} arrgroup select new { typeid = arrgroup.typeid, versionid = (int)arrgroup.max(mx=>mx.versionid), text = arrgroup.text };
but then, returns all four of array elements. therefore, no good. don't max want.
please help!
thanks.
use
text = arrgroup.orderbydescending(mx=>mx.versionid).first().text
full query:
var myarrayquery = arr in array group arr arr.typeid arrgroup select new { typeid = arrgroup.key, versionid = (int)arrgroup.max(mx=>mx.versionid), text = arrgroup.orderbydescending(mx=>mx.versionid).first().text };
Comments
Post a Comment