【发布时间】:2013-02-26 09:11:43
【问题描述】:
您好,我有一个业务逻辑层,可将 selectlistitems 返回给控制器,然后将其传递给视图以填充选择列表。
我有这个方法有效:
public IEnumerable<SelectListItem> GetDevices
{
get
{
using (IDeviceData repository = _dataFactory.Create())
{
return repository.DeviceTypes.ToList()
.Where(dt => dt.ParentId == 10 )
.Select(dt =>
new SelectListItem
{
Text = (dt.Name).Trim(),
Value = dt.Id.ToString()
});
}
}
}
而这不是:
public IEnumerable<SelectListItem> GetGroups(int deviceTypeId)
{
using (IDeviceData repository = _dataFactory.Create())
{
return repository.DeviceTypeConfigurationParameterGroupMaps.ToList()
.Where(cm => cm.DeviceTypeId == deviceTypeId)
.Join(repository.ConfigurationParameterGroups, cm => cm.ConfigurationParameterGroupId, cg => cg.Id, (cm, cg) => new { cm, cg })
.Select(cg =>
new SelectListItem
{
Text = (cg.cg.Name).Trim(),
Value = cg.cg.Id.ToString()
});
}
}
明显的区别是两个表之间的连接,我收到的错误是:
Results View = The type '<>f__AnonymousType0<p,d>' exists in both 'System.Web.dll' and 'EntityFramework.dll'
这是在调试时尝试扩展结果时收到的。任何建议都会受到欢迎,因为我对 LINQ 不太熟悉
【问题讨论】:
-
从您发布的内容看来,这两个 DLL 似乎都包含与您创建为
new { cm, cg }的匿名类型相匹配的类型定义。我想如果你创建了一个辅助类(或使用了一种已经存在的类型)而不是使用匿名类,它可能会起作用 -
谢谢乔安娜,这是问题的一部分。请参阅下面的答案。
标签: asp.net-mvc linq ienumerable