通过linq导航获取相关数据

时间:2018-01-04 11:34:57

标签: c# linq entity-framework-core

我想显示与产品关联的类别名称列表。在我的系统中,产品可以链接到任意数量的类别。这些是我的实体模型:

public class Product
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Info { get; set; }
    public decimal Price { get; set; }
    public List<ProductInCategory> InCategories { get; set; }
}

public class ProductInCategory
{
    public int Id { get; set; }
    public int ProductId { get; set; }
    public int SortOrder { get; set; }
    public int ProductCategoryId { get; set; }
    public Product Product { get; set; } // Nav.prop. to product
    public ProductCategory ProductCategory { get; set; } // Nav.prop. to category
}

public class ProductCategory
{
    public int Id { get; set; }
    public int SortOrder { get; set; }
    public string Title { get; set; }

    [ForeignKey(nameof(ParentCategory))]
    public int? ParentId { get; set; }

    public ProductCategory ParentCategory { get; set; } //nav.prop to parent
    public ICollection<ProductCategory> Children { get; set; } //nav. prop to children

    public List<ProductInCategory> ProductInCategory { get; set; }
}

修改 这是我的UPDATED视图模型:

public class ViewModelProduct
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Info { get; set; }
    public decimal Price { get; set; }
    public int SortOrder { get; set; }
    public List<ProductInCategory> InCategories { get; set; }
    public IEnumerable<ProductCategory> Categories { get; set; }
}

目前,我有此查询,该查询使用InCategories中的类别ID列表检索产品:

var product = _context.Products
            .Select(p => new ViewModelProduct
            {
                Id = p.Id,
                Title = p.Title,
                Info = p.Info,
                Price = p.Price,
                InCategories = p.InCategories
            })
            .SingleOrDefault(m => m.Id == id);

需要添加哪些内容才能Title ProductCategory ProductCategoryId ProductInCategory中的ViewModelProduct

另外,我不确定我是否喜欢方法语法。为了进行连接,我认为查询语法更好,因为可读性。我试图制作查询的查询语法版本,但我不知道如何将其放入var product = from p in _context.Products where p.Id == id select new { p.Id, p.Info, p.Title, p.Price, p.InCategories }; ,因此它在发送到视图时失败:

System.out.println("how many dice dots do u want?");
int dots = s.nextInt();
int dots2 = (int) (6 * Math.random()) + 1;

1 个答案:

答案 0 :(得分:2)

我认为这是您的解决方案: 的修改

public class ViewModelCategoryWithTitle
{
    public int Id { get; set; }
    public string Title { get; set; }
}
public class ViewModelProduct
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Info { get; set; }
    public decimal Price { get; set; }
    public int SortOrder { get; set; }
    public IEnumerable<ViewModelCategoryWithTitle> Categories { get; set; }
}

编辑这是针对IEnumerable ViewModelCategoryWithTitle类别(我的ViewModel):

  var product = (from p in _context.Products
              where p.Id == id
              select new ViewModelProduct
              {
                Id = p.Id,
                Title = p.Title,
                Info = p.Info,
                Price = p.Price,
                Categories = p.InCategories.Select(
                  inCategory=> new ViewModelCategoryWithTitle
                  {
                     CategoryId = inCategory.ProductCategory.Id,
                     Title = inCategory.ProductCategory.Title
                  })
              }).SingleOrDefaultAsync();

编辑这是针对您更新的ViewModel:

  var product = (from p in _context.Products
              where p.Id == id
              select new ViewModelProduct
              {
                Id = p.Id,
                Title = p.Title,
                Info = p.Info,
                Price = p.Price,
                Categories = p.InCategories.Select(
                  inCategory=> inCategory.ProductCategory)
              }).SingleOrDefaultAsync();