我有一个var类型存储LINQ查询的结果,我想将其用作另一个LINQ查询的参数。
我正在使用Controller:
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; }
但我收到错误:&#34;无法投放类型&#39; System.Int32&#39;键入&#39; System.Object&#39;。 LINQ to Entities仅支持转换EDM原语或枚举类型。&#34;
在SQL中我做了类似的事情:
SELECT ParentLocation FROM PartsLocations where LocationName =&#39; aContainer&#39;;
SELECT * FROM PartsLocations,其中LocationID = ParentLocation;
如何将LINQ查询的结果用作单独查询的参数? 提前谢谢!
答案 0 :(得分:0)
这个
var parentLoc = from a in db.PartsLocations
where a.LocationName.Contains(aContainer)
select a.ParentLocation;
不是结果。这是一个查询。
如果需要结果,请运行查询:
var location = parentLoc.Single();
尝试,例如:
var parentLoc = (from a in db.PartsLocations
where a.LocationName == aContainer
select a.ParentLocation).Single();
var locations = (from b in db.PartsLocations
where b.LocationID == parentLoc
select b).ToList();
答案 1 :(得分:0)
如果我理解正确,您正在寻找将parentloc
作为父级位置的地点。您可以使用引用父位置的导航属性PartsLocation.ParentLocation
轻松完成此操作。您应该能够添加该属性并将当前ParentLocation
属性重命名为ParentLocationId
:
var location = (from b in db.PartsLocations
where b.ParentLocation.LocationName.Contains(aContainer)
select b).ToList();