查询使用LINQ嵌套字典 [英] Querying Nested Dictionaries using LINQ

查看:113
本文介绍了查询使用LINQ嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个POS系统,我需要检查每一个终端上的数据库表是否不同步。

I'm developing a POS system and I need to check whether the database tables on each terminal are out of sync.

予维持一个字典终端信息的,其每一个具有包含表ID以CRC为每个表字典。下面是我的本钱到目前为止简要描述(我用VB.NET,但我已经去掉了很多东西,希望能够澄清的东西):

I maintain a Dictionary of terminal information, each of which has Dictionary containing table ids with a CRC for each table. Below is simplified description of what I've got so far (i'm using VB.NET but I've stripped out a lot of stuff to hopefully clarify things):

e.g.   TerminalList = Dictionary(Of Integer, TerminalInfo)

       class TerminalInfo
         TerminalID: Integer
         TableCRCs: Dictionary(Of String, TableInfo)

       class TableInfo
         TableID: String
         CRC: UInt32


       TerminalID: 1
          TableID: A   CRC: aa10
          TableID: B   CRC: 1234

       TerminalID: 2
          TableID: A   CRC: aa10
          TableID: B   CRC: 1234

       TerminalID: 3
          TableID: A   CRC: 12be
          TableID: B   CRC: 1234

是否有可能为我创造一个LINQ查询,将建立不同的TableIDs和清单CRC的?

Is it possible for me to create a LINQ query that will build a list of distinct TableIDs and CRC's?

i.e. A  aa10
     A  12be
     B  1234

如果该查询的次数大于我所感兴趣的话,我知道,终端是不同步的表的数量。我不感兴趣,这终端或表是不同步的,只有是有区别的。

If the count of this query is greater than the number of tables that i'm interested in then I know that a terminal is out of sync. I'm not interested in which terminal or which table is out of sync, only that there is a difference.

TIA,

西蒙

推荐答案

当然。您没有指定的语言,所以我希望C#是好的(我不流利的VB.NET,但我可以帮你,如果你需要帮助,翻译):

Sure. You didn't specify a language so I hope that C# is okay (I'm not fluent in VB.NET but I can help you if you need help translating):

var query = TerminalList.SelectMany(kvp => kvp.Value.TableCRCs.Values)
                        .GroupBy(info => new { info.TableID, info.CRC });

foreach (var result in query) {
    Console.WriteLine(
        String.Format(
            "{0}|{1:x}",
            result.Key.TableID,
            result.Key.CRC
        )
    );
}

这里的关键是使用的SelectMany 扁平化 TerminalInfo 的嵌套枚举s转换1枚举。从那里,它只是一个平常 GROUPBY 操作来获得期望的结果。

The key to this is using SelectMany to flatten the nested enumerations of TerminalInfos into one enumeration. From there it's just a usual GroupBy operation to get the desired result.

这篇关于查询使用LINQ嵌套字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆