【发布时间】:2017-12-08 16:33:16
【问题描述】:
我正在尝试获取产品列表及其评级、cmets 和视图。 PID是没有外键关系的产品ID列。
产品-
Id Name
1 P1
2 P2
评分 -
Id PID Rating
1 1 5
2 1 4
3 2 3
评论-
Id PID Comment
1 1 Good
2 1 Average
3 2 Bad
观看次数 -
Id PID View
1 1 500
2 1 200
3 2 10
我的班级应该是这样的——
Public Class Product{
public int Id { get; set; }
public string Name { get; set; }
public List<Rating> Ratings{ get; set; }
public List<Comments> Comments{ get; set; }
public List<Views> Views{ get; set; }
}
我正在尝试使用 Linq 组加入来获取此信息,以便获得子集合。
IEnumerable<Product> _products = _context.Product.GroupJoin(_context.Rating, p=>p.id, r=>r.PID, (Product, Rating) => new Product(){
//fill fields here
});
但是如何将其他表也分组到单个数据库查询中。
谢谢
【问题讨论】:
-
linq-to-sql 在您提交更改时自动处理对查询的分组。你在问什么?
-
试试这样的代码:IEnumerable
_products = (from p in _context.Product join r in _context.Rating on p.id equals r.id Join s in _context.Sales on p.id等于 s.id select new { p = p, r = r, s = s}).Select(x => new Product() { ..... } -
我想变成课堂上提到的父子格式。那是我组加入的地区。我想简单的连接将所有记录都变成了平面格式。如果我错了,请纠正我。
标签: c# linq lambda linq-to-sql entity