c# - 实体框架组映射表
问题描述
我有三个名为 Products、Product_Category、Categories 的表。Product_Category 有 Id、ProductId 和 CategoryId。我想得到类似的东西
select p.*, group_concat(distinct c.name SEPARATOR ',') categories
from products p
inner join product_category pc
on p.id = pc.product_id
inner join categories c
on pc.category_id = c.id
group by p.id
使用 linq。
我试过了
var result = (from pc in ProductCategory1
join c in Categories1
on pc.CategoryId equals c.Id into table1
from c in table1.ToList()
join p in Products1
on pc.ProductId equals p.Id into table2
from p in table2.ToList()
group c by new {p, c} into g
select new ViewModel
{
products = g.Key.p,
categories = string.Join(", ", g.Key.c.Name)
}).ToList();
但它复制了产品
{
"products": {
"id": 1,
"name": "Active classic boxers ",
"description": "\"There`s a reason why our boxers are a cult favorite # they keep their cool, especially in sticky situations. The quick-drying, lightweight underwear takes up minimal space in a travel pack. An exposed, brushed waistband offers next-to-skin softness, five-panel construction with a traditional boxer back for a classic fit, and a functional fly. Made of 3.7-oz 100% recycled polyester with moisture-wicking performance. Inseam (size M) is 4 1/2\"\". Recyclable through the Common Threads Recycling Program.<br><br><b>Details:</b><ul> <li>\"\"Silky Capilene 1 fabric is ultralight, breathable and quick-to-dry\"\"</li> <li>\"\"Exposed, brushed elastic waistband for comfort\"\"</li> <li>5-panel construction with traditional boxer back</li> <li>\"\"Inseam (size M) is 4 1/2\"\"\"\"\"\"</li></ul><br><br><b>Fabric: </b>3.7-oz 100% all-recycled polyester with Gladiodor natural odor control for the garment. Recyclable through the Common Threads Recycling Program<br><br><b>Weight: </b>99 g (3.5 oz)<br><br>Made in Mexico.\"",
"createdAt": "2016-10-28T14:03:53+03:00",
"updatedAt": "2016-11-06T15:03:53",
"price": 38.94,
"quantity": 6
},
"categories": {
"id": 1,
"name": "Housekeeping"
}
},
{
"products": {
"id": 1,
"name": "Active classic boxers ",
"description": "\"There`s a reason why our boxers are a cult favorite # they keep their cool, especially in sticky situations. The quick-drying, lightweight underwear takes up minimal space in a travel pack. An exposed, brushed waistband offers next-to-skin softness, five-panel construction with a traditional boxer back for a classic fit, and a functional fly. Made of 3.7-oz 100% recycled polyester with moisture-wicking performance. Inseam (size M) is 4 1/2\"\". Recyclable through the Common Threads Recycling Program.<br><br><b>Details:</b><ul> <li>\"\"Silky Capilene 1 fabric is ultralight, breathable and quick-to-dry\"\"</li> <li>\"\"Exposed, brushed elastic waistband for comfort\"\"</li> <li>5-panel construction with traditional boxer back</li> <li>\"\"Inseam (size M) is 4 1/2\"\"\"\"\"\"</li></ul><br><br><b>Fabric: </b>3.7-oz 100% all-recycled polyester with Gladiodor natural odor control for the garment. Recyclable through the Common Threads Recycling Program<br><br><b>Weight: </b>99 g (3.5 oz)<br><br>Made in Mexico.\"",
"createdAt": "2016-10-28T14:03:53+03:00",
"updatedAt": "2016-11-06T15:03:53",
"price": 38.94,
"quantity": 6
},
"categories": {
"id": 5,
"name": "Public Areas"
}
}
解决方案
在 LINQ-to-Entities 中使用join
几乎总是错误的方法。
怎么样:
var query = from p in db.Products
select new ViewModel()
{
product = p,
categories = p.ProductCategories.SelectMany(pc => pc.Category).ToList()
};
?
推荐阅读
- vba - 将两个访问查询导出到一个 excel 文件到各自的工作表上
- r - R for循环与多个数据帧上的if else
- php - Python 相当于 PHPDoc 块?
- javascript - 复选框 JavaScript 不会更改样式表
- sql - 在这些情况下我可以切换到简单恢复吗?
- java - 具有 API 值的倒数计时器
- ios - 无法打开任何 HTTPS 资源 Swift
- php - 在 laravel 5.6 中使用 ajax 序列化 Form 动态图像字段
- java - Selenium - 如何在 iframe 上定位元素
- angular - Angular 模块不适用于 esnext 模块配置