我有一个使用LINQ填充的选择列表,使用以下代码:
public List<string> getCustomerNames()
{
var Customers = (from c in _context.OCRDs
where c.CardType == "c"
select c.CardCode
).ToList();
return Customers;
}
我正在尝试创建一个多列列表,因此'CardCode'可以是值,'CardName'可以作为列表中的描述。
例如:
C001 Name1
C002 Name2
答案 0 :(得分:1)
在此类中创建新的结果类和查询值:
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;
}
答案 1 :(得分:1)
您可以使用Dictionary
代替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;
}