James Sherburn
James Sherburn

Reputation: 99

Include multiple values in Linq list

I have a select list which is populated using LINQ, using the following code:

public List<string> getCustomerNames()
{
     var Customers = (from c in _context.OCRDs
     where c.CardType == "c"
     select c.CardCode
     ).ToList();

     return Customers;
}

I'm trying to create a multiple column list, so that the 'CardCode' can be the value and 'CardName' can act as a description in the list.

For Example:

C001 Name1

C002 Name2

Upvotes: 0

Views: 787

Answers (2)

w.b
w.b

Reputation: 11228

You can use a Dictionary instead of a List:

public Dictionary<string, string> GetCustomerNames()
{ 
     var Customers = (from c in _context.OCRDs
                      where c.CardType == "c"
                      select c)
                      .ToDictionary(c => c.CardCode, c => c.CardName);

     return Customers;
}

Upvotes: 1

Backs
Backs

Reputation: 24903

Create new result class and query values in this class:

class Item
{
    public string CardCode {get;set;}
    public string CardName {get;set;}
}

public List<Item> getCustomerNames()
{
     var Customers = (from c in _context.OCRDs
                      where c.CardType == "c"
                      select new Item { CardCode = c.CardCode, CardName = c.CardName }
                      ).ToList();

     return Customers;
}

Upvotes: 1

Related Questions