【问题标题】:return a dictionary of Tuples as a return type of a function in c#在c#中返回元组字典作为函数的返回类型
【发布时间】:2019-02-01 14:21:48
【问题描述】:

我有一本字典如下:

  var returnedresult = (from v in vaults.Where(v => v.IntegrationTypeInternal == (int)integrationType)
                                  join c in DB.Cobrands on v.Cobrand.ID equals c.ID
                                  join p in DB.TaxPartners on c.ID equals p.Cobrand.ID
                                  join pb in DB.TaxPartnerBranches on p.ID equals pb.Partner.ID
                                  join pa in DB.TaxPartnerAgents on pb.ID equals pa.Branch.ID
                                  join pac in DB.Accounts on pa.Account.ID equals pac.ID
                                  where v.Account == null || v.Account.ID == pac.ID
                                  select new
                                  {
                                      v.Account,
                                      v.Cobrand,
                                      v.CredentialsXml,
                                      pac
                                  }).ToList();


var myDictionary2 = returnedresult.GroupBy(x => new { x.Account, x.Cobrand, x.CredentialsXml },
            (key, group) => new
            {
                key1 = key.Account,
                key2 = key.Cobrand,
                key3 = key.CredentialsXml,
                result = group.Select(g => g.pac).ToList()
            });

是否可以使用 Tuple 代替这个字典键?我想在一个函数中返回这个字典,我不知道如何在不创建新类型作为 dictionary Key

的对象的情况下返回它

【问题讨论】:

    标签: c# dictionary group-by tuples


    【解决方案1】:

    你可以用 Linq 返回任何东西。

    var myDictionary2 = returnedresult.GroupBy(x => new { x.Account, x.Cobrand, x.CredentialsXml },
            (key, group) => Tuple.Create(key.Account, key.Cobrand, key.CredentialsXml, group.Select(g => g.pac).ToList());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-24
      • 2011-02-14
      • 2018-04-06
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      相关资源
      最近更新 更多