首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有返回我期望的结果的LINQ查询

没有返回我期望的结果的LINQ查询
EN

Stack Overflow用户
提问于 2014-10-14 23:15:01
回答 2查看 43关注 0票数 0

希望这只是一个简单的案例,我没有看到一些小细节.

我在玩LINQ的例子。下面是我创建的一个简单类:

代码语言:javascript
复制
    public class Art
    {
        public string Type { get; set; }
        public int Price { get; set; }
    }

下面是我正在创建的示例代码:

代码语言:javascript
复制
    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();
    }

我正在寻找的是一个两栏表,与最高的价格“支付”每种类型的艺术品。所以桌子上应该有:

“绘画”2030

“雕塑”12000

“图标”10

相反,我在第二栏中没有看到任何数字。我遗漏了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-14 23:19:09

而不是找到最大和检查艺术品的那个价格,我只是订购它。我不擅长查询语法,但这就是我要做的。

代码语言:javascript
复制
var artprices = art.GroupBy(c => c.Type)
                   .Select(c => new
                                {
                                    ArtType = c.Key,
                                    MostExpensive = c.OrderByDescending(x => x.Price).First()
                                });

心情不好,但这对你来说可能是最好的选择。

尽管如此,我也不知道你为什么会在第二篇专栏中期望数字。如果你只想要那种艺术的最高价格,而不是艺术本身,那就与你所拥有的不同了。

代码语言:javascript
复制
var artprices = art.GroupBy(c => c.Type)
                   .Select(c => new
                                {
                                    ArtType = c.Key,
                                    MostExpensive = c.Max(x => x.Price)
                                });

如果我对你的理解是正确的,我怀疑你的专栏是在寻找一个数字类型,而你给它一个Art,所以它没有打印任何内容。我可能错了,如果不了解你的设置就很难了。但这是我的猜测。如果是这样的话,我在这里的后一个查询应该会对您有所帮助。

票数 3
EN

Stack Overflow用户

发布于 2014-10-14 23:32:57

表达式枚举结果的匿名类型有成员ArtType、类型字符串和(最重要的) MostExpensive类型,它们具有IEnumerable类型。换句话说,可能存在两个不同的问题:第一,如果希望第二列是单个值,则需要返回单个值,而不是值的枚举。第二,你想要的值的类型是int,而不是Art。

我想你想要的是这样的表达:

代码语言:javascript
复制
var artprices =
    from a in art
    group a by a.Type into g
    select new { ArtType = g.Key, MostExpensive = g.Max(a => a.Price) };
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26371967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档