简体   繁体   English

实体Entity Framework相关记录的访问计数

[英]Access count of related records of an entity Entity Framework

I have two models: 我有两个模型:

public class HouseType
 {
    public int Id { get; set; }
    public string TypeName { get; set; }

    public virtual IEnumerable<HouseModel> HouseModels { get; set; }
 }

and

public class HouseModel
{
    public int Id { get; set; }
    public string ModelName { get; set; }

    [DisplayFormat(DataFormatString = "{0:n2}")]
    public double StandardPrice { get; set; }

    [ForeignKey("HouseType")]
    public int HouseTypeID { get; set; }

    public virtual HouseType HouseType { get; set; }

    public virtual IEnumerable<HouseUnit> HouseUnits { get; set; }
}

I am returning a JSON result, so as expected I cannot manipulate it in a view, because the display is handled by a javascript file that I made. 我返回的是JSON结果,因此按预期,我无法在视图中进行操作,因为显示是由我制作的javascript文件处理的。

I am trying to retrieve the number of HouseModel that is contained by HouseType . 我正在尝试检索HouseModel包含的HouseType I have tried: 我努力了:

db.HouseTypes.Select(h => new
 {
     HouseCount = h.HouseModels.Count()
  }).ToList();

But Entity Framework complains about it. 但是实体框架对此有所抱怨。 How can I access the count of related records inside an entity? 如何访问实体中相关记录的数量? Any help will be much appreciated. 任何帮助都感激不尽。 Thanks. 谢谢。

Use 采用

public virtual ICollection<HouseUnit> HouseUnits { get; set; }

instead of 代替

public virtual IEnumerable<HouseUnit> HouseUnits { get; set; }

Hope this helps. 希望这可以帮助。

Simply speaking, the trouble is that EF is trying to execute the .Select() statement on the db server but, of course, the db server does not know how to create a new object. 简单地说,麻烦在于EF试图在数据库服务器上执行.Select()语句,但是,当然,数据库服务器不知道如何创建新对象。

You first need to bring back the counts then create your objects so something like this should work better: 您首先需要带回计数, 然后创建您的对象,这样类似的事情应该可以更好地工作:

var listOfCounts = db.HouseTypes
    .Select(h => h.HouseModels.Count())
    .ToList()   
    .Select(c => new
    {
         HouseCount = c
    })
    .ToList();

in this example when the first .ToList() is executed the db needs only return a set of numbers (the counts of HouseModels in each HouseType) then we have a List<int> in local memory from which we can create our objects with the second Select statement. 在此示例中,当执行第一个.ToList() ,数据库仅需要返回一组数字(每个HouseType中的HouseModels的计数),那么我们在本地内存中就有一个List<int> ,我们可以使用第二条Select语句。

As an aside... 作为旁白...

It wasn't part of your original question but maybe you'd want to consider a dictionary rather than a list so you have some means of identifying which count of HouseModels belonged to each HouseType? 这不是您最初的问题的一部分,但也许您想考虑使用词典而不是列表,因此您可以通过某种方式来确定每种HouseType属于哪些HouseModels? in which case we could do something like: 在这种情况下,我们可以执行以下操作:

Dictionary<int,string> houseModelCounts = db.HouseTypes
    .ToDictionary(h => h.Id, h => h.HouseModels.Count());

which would give a dictionary keyed with the HouseType Id with values for the count of HouseModels in each type. 这将给出一个以HouseType Id为键的字典,其中包含每种类型中HouseModels的计数值。 I don't know your context though so maybe unnecessary for you? 我不知道您的情况,所以也许对您来说不必要?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM