Linq:返回不同类型的新列表

时间:2016-04-07 11:51:44

标签: c# linq

如果返回类型&#34; AccountItem&#34;,我想过滤并排序到FDKeyValue<>类型的新列表

我试图在没有循环的情况下这样做,我想我可以这样做:

var monthlyList = accountList.Where(x => x.RoleType == "Metric")
     .OrderBy(x => x.EntityName)
     .Select(new FDKeyValue<long, string>{}
     {
         "Field", "Field"
     }
);

这是我使用循环

的方法
var accountList = DBEntity.ReturnAccountListBySearch((int)this.PageLanguageType, "");
var monthlyList = accountList.Where(x => x.RoleType == "Metric").OrderBy(x => x.EntityName).ToList();
this.MonthlyAccountList = new FDKeyValue<long,string>();
foreach (var item in monthlyList)
{
    this.MonthlyAccountList.Add(item.EntityID, item.EntityName);
}

1 个答案:

答案 0 :(得分:1)

此语法必须提供帮助

    var monthlyList = accountList.Where(x => x.RoleType == "Metric")
            .OrderBy(x => x.EntityName)
            .Select(x => new FDKeyValue<long, string>
                {
                 x.EntityID, x.EntityName
              }
            );