【问题标题】:Save LINQ Query As Variable To Call Another LINQ Query将 LINQ 查询另存为变量以调用另一个 LINQ 查询
【发布时间】: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


    【解决方案1】:

    这个

                  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(); 
    

    【讨论】:

    • 我将第二个查询替换为 'var location = parentLoc.Single();'正如你所建议的那样,但现在我得到错误:'传递到字典中的模型项是'System.Int32'类型,但是这个字典需要'System.Collections.Generic.IEnumerable`1[Parts.PartsLocation]类型的模型项'。
    • @David Browne-我实际上需要为我的第二个 LINQ 查询返回一个记录集,以 parentLoc 作为参数。
    • 不过,使用导航属性更加简洁,并且只需要一次数据库往返。
    【解决方案2】:

    如果我理解正确,您正在寻找以parentloc 作为父位置的位置。您可以通过使用指向父位置的导航属性PartsLocation.ParentLocation 轻松做到这一点。您应该能够添加该属性并将当前的ParentLocation 属性重命名为ParentLocationId

    var location = (from b in db.PartsLocations
                    where b.ParentLocation.LocationName.Contains(aContainer)
                    select b).ToList();                             
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      相关资源
      最近更新 更多