Killilea
Killilea

Reputation: 321

Linq: Return a new list of different type

Given a return of type "AccountItem", I want to filter and sort to a new list of type FDKeyValue<>

I am trying to do this without looping and I thought I could do something like this:

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

here is what I have working with a loop

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

Upvotes: 1

Views: 2145

Answers (1)

Rajshekar Reddy
Rajshekar Reddy

Reputation: 18987

This syntax must help

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

Upvotes: 2

Related Questions