This is the data in my database table:

That’s my business object:
public class Unit
{
public Unit()
{
MemberIdList = new List<String>();
}
public String UnitType { get; set; }
public String UnitZoom { get; set; }
public String MemberZoom { get; set; }
public List<String> MemberIdList { get; set; }
}
The whole data from database is fetched and put into a DataTable.
Now comes the Linq Transformation…
After I return a List with 3 Unit objects holding this data:

Now guess how I got the data into the 3 business objects…
that’s the way I would like to know. A hint might be Distinct and IEqualityComparer for the 3 properties…
just an assumption…
UPDATE
Question updated:
Please read the comment in the code 🙂
var groupedCollection = table.AsEnumerable()
.GroupBy(row => new
{
UType = row.Field<string>("UnitType"),
UZoom = row.Field<string>("UnitZoom"),
MZoom = row.Field<string>("MemberZoom"),
MOrder = row.Field<int>("MemberOrder"),
// I DO NOT WANT the MemberOrder to be in the Group Key, but later on I use this Property to order by it…
});
var unitCollection = groupedCollection
.Select(g => new Unit
{
UnitType = g.Key.UType,
UnitZoom = g.Key.UZoom,
MemberZoom = g.Key.MZoom,
MemberIdList = g.Select(r => r.Field<string>("MemberId")).OrderBy(b => g.Key.MOrder).ToList(),
});
List<Unit> units = unitCollection.ToList();
Something like this ?
EDIT:
the call to
.Distinct()is not strictly necessary. It avoids duplicated MemberIds if you can have them, but it really depends on what OP needs.EDIT2:
(according to the last question update)
You are close to the solution:
just change this line of my code:
to: