编辑结果LINQ to Entities

时间:2013-09-27 22:06:21

标签: asp.net-mvc linq entity-framework razor

我有一张名为“Costumers”的桌子和其他名为“Adress”的桌子,所以这就是我想做的事情:

我想在“消费者”和“地址”上“选择”,并使用字段Costumers.Name和Adress.Street填充DropDownList。下拉列表将如下所示:

Jack Jhonsom - Montain View(CA)
罗伯特巴尔默 - 华盛顿(DC)
Mariah Bendley - 摩根敦(WV)

我是怎么做到的?

我正在使用ASP.NET MVC 4 RAZOR,实体框架

2 个答案:

答案 0 :(得分:1)

<强>更新

var items = user.Select(u => string.Format("{0} - {1}", u.Name, u.Address.Street))

答案 1 :(得分:1)

可能是其中一种解决方案有所帮助:

List<SelectListItem> result;

//**If you have relation Customer.Address
var customers = db.Customers.ToList();

var items = (from n in customers 
             select new SelectListItem
             {
               text = String.Format("{0}-{1}", n.Name, n.Address.City),
               value = n.customerID.ToString();
             }).ToList();
result.AddRange(items);

return result;

//If you don't have relation Customer.Address
var customers = db.Customers.ToList();
var addresses = db.Addresses.ToList();
var items = (from n in customers
             join b in addresses on b.addressid equals n.addressid
             select new SelectListItem
             {
               text = String.Format("{0}-{1}", n.Name, b.City),
               value = n.customerID.ToString();
             }).ToList();
result.AddRange(items);

return result;