LINQ到数据集:如何在order by之后获取两列

LINQ到数据集:如何在order by之后获取两列,linq,Linq,我有以下模式: TierId,CountryCode,CountryName 如何使用LINQ来获取 (蒂丽德) 我知道我可以做以下的解决方案,但是有没有办法避免双重订购? 像col3订购col1和col2(粘在一起)之类的东西 var a = from row in ds.Tables[1].AsEnumerable() group row["CountryCode"] by row["TierId"] into countrieCodes

我有以下模式:

TierId,CountryCode,CountryName

如何使用LINQ来获取 (蒂丽德)

我知道我可以做以下的解决方案,但是有没有办法避免双重订购? 像col3订购col1和col2(粘在一起)之类的东西

 var a = from row in ds.Tables[1].AsEnumerable()
         group row["CountryCode"] by row["TierId"]
         into countrieCodes
         select new
             {
                 tierId = Convert.ToInt32(countrieCodes.Key)-1,
                 countrieCodesList = countrieCodes.Select(i => i.ToString()).ToList()
              };

 var b = from row in ds.Tables[1].AsEnumerable()
         group row["CountryName"] by row["TierId"]
         into countrieNames
         select new
         {
            tierId = Convert.ToInt32(countrieNames.Key)-1,
            countrieCodesList = countrieNames.Select(i => i.ToString()).ToList()
         };

var c = from itemA in a
        from itemB in b
        where itemA.tierId == itemB.tierId
        orderby itemA.tierId
        select new {
          TierId = itemA.tierId,
          CountriesA = itemA.countrieCodesList,
          CountriesB = itemA.countrieCodesList,
         };

谢谢,我不知道。尝试将所有内容都压入Linq并不总是最具可读性的方法。其他选择怎么样:

   class TierCountries
   {
       int TierId { get; private set }
       public HashSet<string> CountryCodes { get; private set; }
       public HashSet<string> CountryNames { get; private set; }

       public TierCountries(int tierId)
       {
            TierId = tierId;
            CountryCodes = new HashSet<string>();
            CountryNames = new HashSet<string>();
       }
   }

   var _TierCountryMap = new Dictionary<int, TierCountries>();

   foreach (var row in ds.Tables[1].AsEnumerable())
   {
       int tierId = Convert.ToInt32(row["TierId"]);
       TierCountries tc;
       if (!_TierCountryMap.TryGet(tierId, out tc))
       {
            tc = new TierCountries(tierId);
            _TierCountryMap[tierId] = tc;
       }
       tc.CountryCodes.Add(row["CountryCode"]);
       tc.CountryNames.Add(row["CountryName"]);
   }
class层次国家
{
int TierId{get;private set}
公共哈希集国家代码{get;private set;}
公共HashSet CountryNames{get;private set;}
公共层级国家(国际层级)
{
蒂里德=蒂里德;
CountryCodes=新哈希集();
CountryNames=新的HashSet();
}
}
var_TierCountryMap=新字典();
foreach(ds.Tables[1].AsEnumerable()中的变量行)
{
int tierId=Convert.ToInt32(第[“tierId”行]);
TierCountries tc;
if(!\u TierCountryMap.TryGet(tierId,out tc))
{
tc=新的TierCountries(tierId);
_TierCountryMap[tierId]=tc;
}
tc.CountryCodes.Add(第[“CountryCode”行]);
tc.CountryName.Add(第[“CountryName”]行);
}

没有太多Linq,但只在表上传递一次。

您是在尝试将表交叉连接到自身,还是在
TierId
列中将表内部连接到自身?现在还不清楚您想做什么,从我的猜测来看,您似乎有一个数据库设计问题,而不是一个代码问题。如果您可以重新格式化代码以避免它滚动,这将非常有帮助。为什么这么多的东西都偏右了?(我现在不能自己做,因为我要下火车了,但你自己做是值得的。)缩进。很抱歉给你添麻烦,我接受了你的建议。就我所知,我如何在LINQ中附加两个列,使它们相互依赖并始终排序在一起?@user311130:Mh,不确定您到底想要什么。最好的可能是,如果你问另一个问题的样本。