Reputation: 676
Hopefully this is just a simple case of me not seeing some minor detail...
I'm playing with LINQ examples. Here's a simple class that I created:
public class Art
{
public string Type { get; set; }
public int Price { get; set; }
}
And here's the sample coding that I was creating:
public static void Example0000()
{
List<Art> art = new List<Art>();
art.Add(new Art() { Price = 45, Type = "painting" });
art.Add(new Art() { Price = 123, Type = "sculpture" });
art.Add(new Art() { Price = 2, Type = "icon" });
art.Add(new Art() { Price = 460, Type = "sculpture" });
art.Add(new Art() { Price = 2030, Type = "painting" });
art.Add(new Art() { Price = 10, Type = "icon" });
art.Add(new Art() { Price = 700, Type = "painting" });
art.Add(new Art() { Price = 1400, Type = "sculpture" });
art.Add(new Art() { Price = 46, Type = "painting" });
art.Add(new Art() { Price = 12000, Type = "sculpture" });
art.Add(new Art() { Price = 6, Type = "icon" });
art.Add(new Art() { Price = 810, Type = "sculpture" });
art.Add(new Art() { Price = 250, Type = "painting" });
art.Add(new Art() { Price = 3, Type = "icon" });
art.Add(new Art() { Price = 1000, Type = "painting" });
art.Add(new Art() { Price = 260, Type = "sculpture" });
var artprices =
from a in art
group a by a.Type into g
let maxPrice = g.Max(a => a.Price)
select new { ArtType = g.Key, MostExpensive = g.Where(a => a.Price == maxPrice) };
dataGridView1.DataSource = artprices.ToArray();
}
What I am looking for is a two column table, with the highest price "paid" for each type of artwork. So the table should have:
"painting " 2030
"sculpture" 12000
"icon" 10
Instead, I am not seeing any numbers in the second column. What am I missing?
Upvotes: 0
Views: 54
Reputation: 70652
The anonymous type of the enumeration result of your expression has members ArtType, having type string, and (most important) MostExpensive, having type IEnumerable<Art>. In other words, there are potentially two different problems: first, if you want the second column to be a single value, you need to return a single value, not an enumeration of values. Second, the type of the value you want is int, not Art.
I think what you want is this expression instead:
var artprices =
from a in art
group a by a.Type into g
select new { ArtType = g.Key, MostExpensive = g.Max(a => a.Price) };
Upvotes: 0
Reputation: 13286
Rather than finding the max and checking for artwork of that price, I would just order it. I'm not great with query syntax, but this is what I would do.
var artprices = art.GroupBy(c => c.Type)
.Select(c => new
{
ArtType = c.Key,
MostExpensive = c.OrderByDescending(x => x.Price).First()
});
Having sorts is always unfortunate, but it's probably the best for your case here.
That all said, I'm also not sure why you'd be expecting numbers in your second column. If you just want the maximum price for that art type, and not the art itself, that's different from what you have.
var artprices = art.GroupBy(c => c.Type)
.Select(c => new
{
ArtType = c.Key,
MostExpensive = c.Max(x => x.Price)
});
My suspicion, if I am understanding you correctly, is that your column is looking for a number type, and you're feeding it an Art
, so it's printing out no contents. I could be wrong, it's hard without knowing a bit more about your setup. But that's my guess. If that's the case, my latter query here should help you.
Upvotes: 3