【发布时间】:2017-06-29 20:36:56
【问题描述】:
我有一个 var 类型存储 LINQ 查询的结果,我想将其用作另一个 LINQ 查询的参数。
我正在使用控制器:
public async Task<ActionResult> Index(String aContainer)
{
var partsLocations = db.PartsLocations.Include(p => p.LocationType).Include(p => p.ManufacturingPlant);
var empty = from y in db.PartsLocations select y;
if (!String.IsNullOrEmpty(aContainer))
{
var parentLoc = from a in db.PartsLocations
where a.LocationName.Contains(aContainer)
select a.ParentLocation;
var location = (from b in db.PartsLocations.Where(b => parentLoc.Equals(b.LocationID))
select b).ToList();
return View( location.ToList());
}
empty = empty.Where(r => r.LocationName.Contains(null));
return View(await empty.ToListAsync());
}
我的观点是:
@model IEnumerable<Parts.PartsLocation>
@{
ViewBag.Title = "Index";
}
型号:
public int LocationID { get; set; }
public int TypeID { get; set; }
public string LocationName { get; set; }
public string Description { get; set; }
public int PlantID { get; set; }
public Nullable<int> BayID { get; set; }
public Nullable<int> SecurityGroupID { get; set; }
public Nullable<int> ParentLocation { get; set; }
public Nullable<System.DateTime> LastMoved { get; set; }
public string Notes { get; set; }
但我收到错误:“无法将类型 'System.Int32' 转换为类型 'System.Object'。LINQ to Entities 仅支持转换 EDM 基元或枚举类型。”
在 SQL 中,我执行以下操作:
SELECT ParentLocation FROM PartsLocations where LocationName = 'aContainer';
SELECT * FROM PartsLocations where LocationID = ParentLocation;
如何将 LINQ 查询的结果用作单独查询的参数? 提前致谢!
【问题讨论】:
标签: c# entity-framework linq