|
Hi,
Totally new to LINQ and wondering how I can get the following sandbox code to work. Ideas?
List<PositionValues> list = new List<PositionValues>();
PositionValues item1 = new PositionValues();
item1.Position = 2;
item1.Value = 44;
list.Add(item1);
PositionValues item2 = new PositionValues();
item2.Position = 1;
item2.Value = 33;
list.Add(item2);
var resultList = from b in list group b by b.Position into g orderby b.Value select b;
It relies on:
public struct PositionValues
{
/// <summary>
/// The position.
/// </summary>
public int Position;
/// <summary>
/// The position.
/// </summary>
public double Value;
}
Thanks for looking,
Alan
|
|
|
|
|
It might be helpful to tell us what isnt working, or what you expect to happen.
|
|
|
|
|
Biggest thing that looks wrong to me is that your not using the groupby properly
have a look at this to for a good example 101 LINQ Samples in C#[^]
If that dont work please tell us as much information as possible and we will help you as much as we can possible.
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
Hi J4amieC and Simon_Whale,
Sorry I did not respond sooner. I started to look other directions and was paying less attention to this thread.
For the code supplied, I am getting the compiler error:
"The name 'b' does not exist in the current context"
If I could get rid of the error, I am hoping that the code would do two sorts:
A primary sort based on "Position" and a secondary sort on "Value". I would end up with the list grouped on "Position" and within each "Position" groupings they would be sorted based on "Value". If the following code snippet is added for a 3rd item:
PositionValues item3 = new PositionValues();
item3.Position = 1;
item3.Value = 22;
list.Add(item3);
the resulting list (I am hoping) would be ordered as follows:
resultList[0] {1, 22}
resultList[1] {1, 33}
resultList[2] {2, 44}
where the first entry in the parenthesis is the "Position" and the second is the "Value".
I had looked previously at the 101 LINQ Samples but did not see something readily that was close enough to this (but probably just missed it since new to this - good suggestion and I plan on using as reference).
Thanks again,
Alan
|
|
|
|
|
Since responses have ended, I will post elsewhere. Thanks for the replies.
|
|
|
|