我有以下数据结构(事先道歉,因为我不确定在SO中表示数据结构的最佳方式).以下是链接的表格列表(由一对多表示
1
|
8
Run Code Online (Sandbox Code Playgroud)
关系.
---------------------
|GlobalListTable: |
|-------------------|
|Id |
|ProductGroupTableId|
|ProductListTypeId |
---------------------
8
|
1
---------------------
|ProductGroupTable: |
|-------------------|
|Id |
|Name |
---------------------
1
|
8
---------------------
|ProductTable: |
|-------------------|
|Id |
|Name |
|ProductGroupTableId|
---------------------
1
|
8
---------------------
|ComponentTable: |
|-------------------|
|Id |
|Name |
|ProductTableId |
|ComponentTypeId |
---------------------
Run Code Online (Sandbox Code Playgroud)
最简单形式的数据看起来像这样
GlobalListTable1
ProductGroupTable
ProductTable1
ComponentTable ComponentTypeId1
ComponentTable ComponentTypeId2
ComponentTable ComponentTypeId3
ComponentTable ComponentTypeId4
ProductTable2
ComponentTable ComponentTypeId1
ComponentTable ComponentTypeId3
ProductTable3
ComponentTable ComponentTypeId3
ComponentTable ComponentTypeId4
Run Code Online (Sandbox Code Playgroud)
我想要做的是查询(在lambda中)数据并返回数据,但用ProductListTypeId和过滤ComponentTypeId
所以例如我有第一个(简单)位
var productListTypeId=1;
var componentTypeId=4;
var _results=this.Context.GlobalListTable
.Where(i=>i.ProductListTypeId==productListTypeId);
Run Code Online (Sandbox Code Playgroud)
我试过添加
.Where(i=>i.ProductGroupTable.ProductTable.ComponentTable.ComponentTypeId == componentTypeId);
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.
我想传入(说)上面的参数并返回以下内容:
GlobalListTable1
ProductGroupTable
ProductTable1
ComponentTable4
ProductTable3
ComponentTable4
Run Code Online (Sandbox Code Playgroud)
编辑:使用EntityFramework检索数据
编辑:我开始认为这对于标准的linq查询是不可能的.我似乎能够使这个工作的唯一方法是遍历查询并手动删除不需要的记录.
这就是我最终解决问题的方法。
看起来我无法单独使用 linq 来实现它,因此我需要迭代结果并消除不需要的结果。
我获取顶级对象的查询:
var query = this.Context.GlobalListTable
.Where(i => i.ProductListTypeId == productListTypeId)
.Select(i => i.ProductGroupTable)
.SelectMany(i => i.ComponentTables)
.Where(t => t.ComponentTypeId == componentTypeId)
.ToList();
Run Code Online (Sandbox Code Playgroud)
迭代结果并排除不需要的:
foreach (var _globalListTable in query)
{
foreach (var _productTable in _globalListTable.ProductGroupTable.ProductTables)
{
var _exclude = _productTable.ComponentTables.Where(i => i.ComponentTypeId != componentTypeId);
_productTable.ComponentTables = _productTable.ComponentTables.Except(_exclude).ToList();
}
}
return query;
Run Code Online (Sandbox Code Playgroud)
即使不优雅,也能完美地工作。
| 归档时间: |
|
| 查看次数: |
349 次 |
| 最近记录: |