| | |
LINQ - getting IEnumerables
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 279
Reputation:
Solved Threads: 31
I'm having a hard time geting access to properties in the var output of the query (I know I can use foreach on the result that I've got but I'm not sure which elements values I'm accessing). I think my query is just plain wrong, so I'd appreciate some help.
I have a class which contains a Point struct (X,Y). I want to group all of the objects by their X value, and then arrange each of those groups in descending order by the Y values within them. I can get the grouping just fine, but placing the values into the group seems to block me from doing any further computations with them.
Here is my LINQ query:
at the end of the "group.... into g" line, le goes out of scope so I can't get access to the Y component anymore.
This code currently gives me a group of collections sorted by their X value. I've even tried joining it with the original data set so I could just get a set of (X,Y) values back.
So my question is twofold, how can I "get" my Y values out of the the "g" list or how could I reformulate my query better in the first place...
Thanks much for any help!
I have a class which contains a Point struct (X,Y). I want to group all of the objects by their X value, and then arrange each of those groups in descending order by the Y values within them. I can get the grouping just fine, but placing the values into the group seems to block me from doing any further computations with them.
Here is my LINQ query:
C# Syntax (Toggle Plain Text)
var col = from le in mylist group le by le.X into g orderby g.Key descending select g;
at the end of the "group.... into g" line, le goes out of scope so I can't get access to the Y component anymore.
This code currently gives me a group of collections sorted by their X value. I've even tried joining it with the original data set so I could just get a set of (X,Y) values back.
So my question is twofold, how can I "get" my Y values out of the the "g" list or how could I reformulate my query better in the first place...
Thanks much for any help!
Try to group first then order second
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
C# Syntax (Toggle Plain Text)
var col = from le in mylist group le by le.X into g orderby g.Key descending select g;
c# Syntax (Toggle Plain Text)
var colGrouped = from le in mylist group le by le.X select le; var colOrdered = from colGrouped ..... orderby...
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Sep 2009
Posts: 279
Reputation:
Solved Threads: 31
•
•
•
•
On two phases
c# Syntax (Toggle Plain Text)
var colGrouped = from le in mylist group le by le.X select le; <-----* var colOrdered = from colGrouped ..... orderby...
So please send your class code you want to group and order it; the class has this struct to play with this problem.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Sep 2009
Posts: 279
Reputation:
Solved Threads: 31
Here's the class (it's a game of space invaders from Head First C#)
Here's where the query happens in another class called Game (there's a bit more to the method but it's not relevant). The second query as it is doesn't compile...
C# Syntax (Toggle Plain Text)
public class Invader { //see directionandtypeenum.cs for Type definition private const int HorizontalInterval = 10; private const int VerticalInterval = 25; //40; private Bitmap image; private Point location; public Point Location { get { return location; } } //changed this to make life easier public Type InvaderType { get; private set; } public Rectangle Area { get { return new Rectangle(location, image.Size); } } public int Score { get; private set; } public Invader(Type InvaderType,Point location,int score) { this.InvaderType = InvaderType; this.location = location; this.Score = score; image = InvaderImage(0); } public void Draw(Graphics g,int animationCell) { g.DrawImage(InvaderImage(animationCell), Location); } public void Move(Direction direction) { switch (direction) { case Direction.Down: location.Y += VerticalInterval; break; case Direction.Left: location.X -= HorizontalInterval; break; case Direction.Right: location.X += HorizontalInterval; break; default: break; } } private Bitmap InvaderImage(int animationCell) { return (Bitmap)Properties.Resources.ResourceManager.GetObject(InvaderType.ToString().ToLower() + (animationCell+1).ToString()); } }
Here's where the query happens in another class called Game (there's a bit more to the method but it's not relevant). The second query as it is doesn't compile...
C# Syntax (Toggle Plain Text)
private void FireInvaderShots() { if (invaderShots.Count <=2) { var Column = from inv in invaders group inv by inv.Location.X into g select g; //into invadergroup //orderby invadergroup.Key descending var cols = from v in Column where v.ElementAt(v.Key).Location.Y == v.ElementAt(v.Key).Location.Y.Max() select v; List<Invader> col = new List<Invader>();
Last edited by jonsca; Sep 19th, 2009 at 4:10 am.
Take a look at this code,
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; class MainApp { static void Main() { List<MyData> items = new List<MyData>(); items.Add(new MyData("A", "1")); items.Add(new MyData("A", "2")); items.Add(new MyData("B", "3")); items.Add(new MyData("A", "4")); var result = from all in items group all by all.State into grpResult orderby grpResult.Key descending select grpResult; foreach (var t in result){ Console.WriteLine(t.Key + " " + t.Count()); } } } public class MyData{ public string State { get; set; } public string City { get; set; } public MyData() { } public MyData(string _state, string _city){ State = _state; City = _city; } }
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: Sep 2009
Posts: 279
Reputation:
Solved Threads: 31
Nice! Thanks so much. Here's what I ended up with(I had to make some minor adjustments, but otherwise worked like a charm):
Thanks to Ramy, also.
C# Syntax (Toggle Plain Text)
var Column = from all in invaders group all by all.Location.X into grpResult orderby grpResult.ElementAt(0).Location.Y descending select grpResult; foreach (var v in Column) Console.WriteLine(v.ElementAt(v.Count()-1).Location.ToString());
Thanks to Ramy, also.
•
•
•
•
C# Syntax (Toggle Plain Text)
var Column = from all in invaders group all by all.Location.X into grpResult orderby grpResult.ElementAt(0).Location.Y descending select grpResult; foreach (var v in Column) Console.WriteLine(v.ElementAt(v.Count()-1).Location.ToString());
v.Last() instead of v.ElementAt(v.Count()-1) . Using v.Last() also only iterates through the collection once, which is sometimes something to be aware of (and sometimes a needless microoptimization) and anyway it's less code.Edit: And I think you have it all wrong. Your code groups by the X value and sorts the collection of groups by the groups' first Y values. That's what you want?
Last edited by Rashakil Fol; Sep 19th, 2009 at 8:24 pm.
All my posts may be redistributed under the GNU Free Documentation License.
![]() |
Similar Threads
- LINQ stored procedure to select all data from table to datagridview (C#)
- LINQ insert method problem, .NET 3.5, VS2008, LINQ (ASP.NET)
- LINQ & XML (references & using System..) (C#)
- Comparing data from database using LINQ (ASP.NET)
- [LINQ, C#]Problem with autogenerated DataClasses.designer.cs (ASP.NET)
- Zip-Linq Rectractable RJ45 Crossover Cable: How do I connect? (Networking Hardware Configuration)
Other Threads in the C# Forum
- Previous Thread: DateTime.. out_of_range
- Next Thread: import from excel to datagridview
| Thread Tools | Search this Thread |
.net access algorithm angle array asp.net barchart bitmap box broadcast c# capturing check checkbox client combobox control conversion csharp custom database datagrid datagridview dataset datetime dbconnection degrees delegate design development disappear draganddrop drawing encryption enum eventhandlers excel file firefox form format forms function gdi+ image index input install java label leak libraries list listbox loop mandelbrot math monodevelop mouseclick msword mysql operator path pause photoshop picturebox pixelinversion post programming radians regex remoting resourcefile richtextbox round server sleep socket sql statistics stream string table tcpclientchannel text textbox thread time timer update usercontrol validation virtualization visualbasic visualstudio webbrowser windows winforms wpf xml






