The problem is: I got a list of elements and then I use group by. I need to get another list where contains the half of elements from each group by.
How can I do that? I’m using LINQ.
UPDATE:
This is the first list that I get.
XDocument xdoc = XDocument.Load(path);
var conditions = from c in xdoc.Descendants("Condition")
select new
{
ObjectiveID = (int)c.Attribute("ObjectiveID"),
TypeID = (int)c.Attribute("TypeID"),
ProblemID = (int)c.Attribute("ProblemID"),
Ranges = (from r in c.Descendants("Range")
select new
{
Decimals = (int)r.Attribute("Decimals"),
Min = (decimal)r.Attribute("Min"),
Max = (decimal)r.Attribute("Max")
}).ToArray(),
};
That’s the original which I’m using. From that one, I only want to get the half of problems from each OBJECTIVEID.
If in the enummerable I have 2 elements of the same objectiveID, I must get only one. If I got one problem, I must get only one, If I have 5 I’d have 2 or 3.
I’m not sure what you’re asking — are you trying to get individual elements from each group into another list? If so, SelectMany is probably what you’re looking for.
Alternatively:
Response to Edit
There’s an overload of Select that also includes an integer index — you can use this to filter out all the odd or even items to get half of the set.
You could change it to something like
I’m beginning to think I don’t understand the question. If the problem is that you have data like
and you don’t want two different problemIDs for the same objectiveID, you can use GroupBy / SelectMany / Take to narrow down to only one problem per objectiveID