返回元组字典作为c#中函数的返回类型

时间:2018-08-27 04:39:42

标签: c# dictionary group-by tuples

我有以下字典:

  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()
            });

是否可以使用元组代替此词典密钥?我想在函数中返回此字典,而且我不知道如何在不为字典键

创建新类型的情况下返回字典

1 个答案:

答案 0 :(得分: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());