[英]Linq query to return filtered data
我有以下數據結構(事先道歉,因為我不確定在SO中表示數據結構的最佳方式)。 以下是鏈接的表格列表(由一對多表示
1
|
8
關系。
---------------------
|GlobalListTable: |
|-------------------|
|Id |
|ProductGroupTableId|
|ProductListTypeId |
---------------------
8
|
1
---------------------
|ProductGroupTable: |
|-------------------|
|Id |
|Name |
---------------------
1
|
8
---------------------
|ProductTable: |
|-------------------|
|Id |
|Name |
|ProductGroupTableId|
---------------------
1
|
8
---------------------
|ComponentTable: |
|-------------------|
|Id |
|Name |
|ProductTableId |
|ComponentTypeId |
---------------------
最簡單形式的數據看起來像這樣
GlobalListTable1
ProductGroupTable
ProductTable1
ComponentTable ComponentTypeId1
ComponentTable ComponentTypeId2
ComponentTable ComponentTypeId3
ComponentTable ComponentTypeId4
ProductTable2
ComponentTable ComponentTypeId1
ComponentTable ComponentTypeId3
ProductTable3
ComponentTable ComponentTypeId3
ComponentTable ComponentTypeId4
我想要做的是查詢(在lambda中)數據並返回數據,但過濾了ProductListTypeId
和ComponentTypeId
所以例如我有第一個(簡單)位
var productListTypeId=1;
var componentTypeId=4;
var _results=this.Context.GlobalListTable
.Where(i=>i.ProductListTypeId==productListTypeId);
我試過添加
.Where(i=>i.ProductGroupTable.ProductTable.ComponentTable.ComponentTypeId == componentTypeId);
但這似乎不起作用。
我想傳入(說)上面的參數並返回以下內容:
GlobalListTable1
ProductGroupTable
ProductTable1
ComponentTable4
ProductTable3
ComponentTable4
編輯:使用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();
迭代結果並排除不需要的內容:
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;
如果不是優雅的話,效果很好。
SelectMany會做的伎倆,這是查詢:
var global = new List<GlobalListTable>()
{
new GlobalListTable()
{
ProductListTypeId = 1,
ProductGroupTable = new ProductGroupTable()
{
ProductTables = new List<ProductTable>()
{
new ProductTable()
{
ComponentTables = new List<ComponentTable>()
{
new ComponentTable(){ComponentTypeId = 4, Name = "Sucess"}
}
}
}
}
}
};
var productListTypeId=1;
var componentTypeId=4;
var query =
global.Where(t => t.ProductListTypeId == productListTypeId)
.Select(t => t.ProductGroupTable)
.SelectMany(t => t.ProductTables)
.SelectMany(t => t.ComponentTables)
.Where(t => t.ComponentTypeId == componentTypeId);
編輯如果你需要globalListTable,那么查詢將如下所示:
var query =
global.Where(t => t.ProductListTypeId == productListTypeId).Where(t1=>t1
.ProductGroupTable
.ProductTables
.SelectMany(t => t.ComponentTables)
.Any(t => t.ComponentTypeId == componentTypeId));
EDIT2
var filterComp =
global.Select(t => t.ProductGroupTable)
.SelectMany(t => t.ProductTables)
.SelectMany(t => t.ComponentTables)
.Where(t => t.ComponentTypeId == componentTypeId);
我使用了像這樣定義的poco類:
internal class GlobalListTable
{
public ProductGroupTable ProductGroupTable { get; set; }
public int ProductListTypeId { get; set; }
}
internal class ProductGroupTable
{
public List<ProductTable> ProductTables { get; set; }
}
internal class ProductTable
{
public List<ComponentTable> ComponentTables { get; set; }
}
internal class ComponentTable
{
public string Name { get; set; }
public int ComponentTypeId { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.