简体   繁体   English

实体框架:选择不同的对象

[英]Entity Framework: Selecting distinct objects

I am using Entity Framework for my data access and manipulation. 我正在使用实体框架进行数据访问和操作。 I have the following class: 我有以下课程:

public class ProductCategory
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    [MinLength(5)]
    [MaxLength(45)]
    public string Name { get; set; }

    [Column("ParentId", TypeName = "int")]
    public int? ProductCategoryId { get; set; }
    public virtual ProductCategory Parent { get; set; }
    public ICollection<ProductCategory> SubCategories { get; set; }
}

Now I am querying using LINQ to get a list of all categories, including the related child categories (using the ParentId column) like this: 现在,我正在使用LINQ查询以获取所有类别的列表,包括相关的子类别(使用ParentId列),如下所示:

public async Task<IQueryable<ProductCategory>> GetAll()
{
    return await Task.Run(() => {
        return _db.ProductCategories.AsQueryable();
    });
}

When I run this code, I get the following result: 运行此代码时,将得到以下结果:

[
  {
    "Id": 1,
    "Name": "Computers & Software",
    "Parent": null,
    "SubCategories": [
      {
        "Id": 2,
        "Name": "Tablets & E-Readers",
        "SubCategories": [
          {
            "Id": 8,
            "Name": "Apple iPad",
            "SubCategories": []
          },
          {
            "Id": 9,
            "Name": "Samsung",
            "SubCategories": []
          },
          {
            "Id": 10,
            "Name": "Other makes",
            "SubCategories": []
          }
        ]
      }
    ]
  },
  {
    "Id": 2,
    "Name": "Tablets & E-Readers",
    "Parent": {
      "Id": 1,
      "Name": "Computers & Software",
      "Parent": null
    },
    "SubCategories": [...]
  }
]

Now, the thing is that the second object with the name "Tablets & E-Readers" already exists in the array of "SubCategories" within the first object of the array. 现在,问题是名称为“ Tablets&E-Readers”的第二个对象已经存在于该数组的第一个对象内的“ SubCategories”数组中。 I would like to not see this second object (DISTINCT), but cannot get this working. 不想看到第二个对象(DISTINCT),但是无法正常工作。 Does anyone know how to tackle this issue? 有谁知道如何解决这个问题?

I think that there is something wrong with your data. 我认为您的数据有问题。 The subcategory ( Tablets & E-Readers ) should not have a null Parent as you have in your data now. 子类别( 平板电脑和电子阅读器不应像您现在在数据中那样具有空的 Parent。 Subcategory mean that there a parent . 子类别意味着有一个父项 If want only the parent category, you can try something like this: 如果只需要父类别,则可以尝试如下操作:

_db.ProductCategories.Where(cat=>cat.Parent == null).AsQueryable();

May be you will get what you want. 也许你会得到你想要的。 It'will not select subcategories in the main list. 它不会在主列表中选择子类别。

Those extra categories are there, because they are linked in the database. 这些额外的类别在那里,因为它们在数据库中链接。

You will need to write a query to filter them out, for example using the LINQ function Except 您将需要编写查询以将其过滤掉,例如使用LINQ函数Except

IQueryable<ProductCatgory> parents = 
(    
    from ProductCategory pc in _db.ProductCategories
    where pc.Parent == null
    select pc
);

return (from ProductCategory pc in parents
        select new 
        { 
            Category = pc,
            SubCategories = pc.SubCategories.Except(parents)
        });

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

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