复杂的 LINQ 查询

发布于 2024-11-01 17:31:32 字数 1452 浏览 0 评论 0原文

考虑到这种结构...

List<IEnumerable<KeyValuePair<String, String>>>

您将如何为以下伪代码编写 LINQ 查询...

SELECT
    /* Count of how many [KeyValuePair] exists in [List] */
FROM
    [List]
WHERE
        [KeyValuePair].Key == "foo"
    AND Int32.Parse([KeyValuePair].Value.Replace(".", "")) > 10

...?


更新

上述查询的结果,根据下面的列表运行,应该是 6(六)...

var list = new List<IEnumerable<KeyValuePair<String, String>>>
{
    new []
    {
        new KeyValuePair<String, String>("foo", "1.1"),
        new KeyValuePair<String, String>("foo", "1.2"),
        new KeyValuePair<String, String>("foo", "1.3")
    },                                            

    new []                                        
    {                                             
        new KeyValuePair<String, String>("foo", "0.1"),
        new KeyValuePair<String, String>("foo", "0.2"),
        new KeyValuePair<String, String>("foo", "0.3")
    },                                            

    new []                                        
    {                                             
        new KeyValuePair<String, String>("foo", "2.1"),
        new KeyValuePair<String, String>("foo", "2.2"),
        new KeyValuePair<String, String>("foo", "2.3")
    }
};

Considering this structure...

List<IEnumerable<KeyValuePair<String, String>>>

How would you write a LINQ query for the following pseudo-code...

SELECT
    /* Count of how many [KeyValuePair] exists in [List] */
FROM
    [List]
WHERE
        [KeyValuePair].Key == "foo"
    AND Int32.Parse([KeyValuePair].Value.Replace(".", "")) > 10

...?

Update

The result of the above query, run aginst the list below, should be 6 (six)...

var list = new List<IEnumerable<KeyValuePair<String, String>>>
{
    new []
    {
        new KeyValuePair<String, String>("foo", "1.1"),
        new KeyValuePair<String, String>("foo", "1.2"),
        new KeyValuePair<String, String>("foo", "1.3")
    },                                            

    new []                                        
    {                                             
        new KeyValuePair<String, String>("foo", "0.1"),
        new KeyValuePair<String, String>("foo", "0.2"),
        new KeyValuePair<String, String>("foo", "0.3")
    },                                            

    new []                                        
    {                                             
        new KeyValuePair<String, String>("foo", "2.1"),
        new KeyValuePair<String, String>("foo", "2.2"),
        new KeyValuePair<String, String>("foo", "2.3")
    }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余罪 2024-11-08 17:31:32

我从中得到6分...

var result = list.Sum(item=>item.Count(kp=>kp.Key == "foo" && int.Parse(kp.Value.Replace(".",String.Empty))>10));

I get 6 from this...

var result = list.Sum(item=>item.Count(kp=>kp.Key == "foo" && int.Parse(kp.Value.Replace(".",String.Empty))>10));
只是我以为 2024-11-08 17:31:32
var result = list.SelectMany(ary =>aryx).Count(item => item.Key == "Foo" && Int32.Parse(item.Value.Replace(".", "")) > 10);

虽然我猜你正在替换“。”因为您想摆脱千位分隔符,所以您可能需要: Int32.Parse(item.Value, NumberStyles.AllowThousands, CultureInfo.CurrentCulture) 确保正确设置了当前区域性。 Strike>

为了清楚起见,我可能会这样写:

var result = list.SelectMany(ary => ary)
                 .Where(item => item.Key.Equals("Foo", StringComparison.CurrentCulture))
                 .Select(item => 
                      Int32.Parse(item.Value.Replace(".", ""))
                 .Count(value=> value> 10);

理解语法可能如下所示:

var q = from @array in list
        from kvp in array
        where kvp.Key.Equals("Foo", StringComparison.CurrentCulture)
        select .Parse(item.Value.Replace(".", ""));
var result = q.Count(value => value > 10);
var result = list.SelectMany(ary =>aryx).Count(item => item.Key == "Foo" && Int32.Parse(item.Value.Replace(".", "")) > 10);

Although I'm guessing you're replacing the "." because you want to get rid of thousands separators, so You might want: Int32.Parse(item.Value, NumberStyles.AllowThousands, CultureInfo.CurrentCulture) making sure you have the Current Culture set appropriately.

I might write it for clarity like this:

var result = list.SelectMany(ary => ary)
                 .Where(item => item.Key.Equals("Foo", StringComparison.CurrentCulture))
                 .Select(item => 
                      Int32.Parse(item.Value.Replace(".", ""))
                 .Count(value=> value> 10);

Comprehension syntax might look like:

var q = from @array in list
        from kvp in array
        where kvp.Key.Equals("Foo", StringComparison.CurrentCulture)
        select .Parse(item.Value.Replace(".", ""));
var result = q.Count(value => value > 10);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文