LINQ查询返回过滤后的数据 [英] Linq query to return filtered data

查看:134
本文介绍了LINQ查询返回过滤后的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的数据结构(提前道歉,因为我不知道的来表示这样的数据结构的最佳方式)。
以下是链接表的列表(由一对多

I have the following data structure (apologies in advance as I'm not sure of the best way to represent the data structure in SO). The following is a list of tables linked (represented by the one to many

1
|
8

关系。

---------------------
|GlobalListTable:   |
|-------------------|
|Id                 |
|ProductGroupTableId|
|ProductListTypeId  |   
---------------------
         8
         |
         1
---------------------
|ProductGroupTable: |
|-------------------|
|Id                 |
|Name               |
---------------------
         1
         |
         8
---------------------
|ProductTable:      |
|-------------------|
|Id                 |
|Name               |
|ProductGroupTableId|
---------------------
         1
         |
         8
---------------------
|ComponentTable:    |
|-------------------|
|Id                 |
|Name               |
|ProductTableId     |
|ComponentTypeId    |
---------------------

在它的数据最简单的形式是这样的。

The data in it's simplest form would look like this

GlobalListTable1
   ProductGroupTable
       ProductTable1
          ComponentTable ComponentTypeId1
          ComponentTable ComponentTypeId2
          ComponentTable ComponentTypeId3
          ComponentTable ComponentTypeId4
        ProductTable2
          ComponentTable ComponentTypeId1
          ComponentTable ComponentTypeId3
       ProductTable3   
          ComponentTable ComponentTypeId3
          ComponentTable ComponentTypeId4

我想要做的就是查询(以拉姆达)数据和 ProductListTypeId ComponentTypeId

What I want to do is to query (in lambda) the data and return the data but filtered by ProductListTypeId and ComponentTypeId

因此,例如我有第一个(简单)位

So for example I have for the first (easy) bit

var productListTypeId=1;    
var componentTypeId=4;
var _results=this.Context.GlobalListTable
.Where(i=>i.ProductListTypeId==productListTypeId);



我曾尝试加入

I have tried adding

.Where(i=>i.ProductGroupTable.ProductTable.ComponentTable.ComponentTypeId == componentTypeId);



但是,这似乎并没有工作。

But that doesn't seem to work.

我想传递(说)以上参数,并具有下列返回:

I would like to pass in (say) the above parameters and have the following returned:

GlobalListTable1
   ProductGroupTable
       ProductTable1
          ComponentTable4
       ProductTable3   
          ComponentTable4

编辑:使用的EntityFramework检索数据。

Using EntityFramework to retrieve the data

编辑:我开始认为这是不可能的使用标准LINQ查询。我似乎能够得到这个工作的唯一方法是通过查询遍历并手动删除不需要的记录。

I'm beginning to think this isn't possible with standard linq queries. The only way I seem to be able to get this working is to iterate through the query and manually remove the unwanted records.

推荐答案

这是我最终解决问题。

它看起来并不像我可以单独使用LINQ实现它,所以我需要通过的结果进行迭代并消除不必要的

It doesn't look like I can achieve it using linq alone so I need to iterate through the results and eliminate the unwanted.

我的查询来获取顶级对象:

My query to get the top level objects:

var query = this.Context.GlobalListTable
        .Where(i => i.ProductListTypeId == productListTypeId)
        .Select(i => i.ProductGroupTable)
        .SelectMany(i => i.ComponentTables)
        .Where(t => t.ComponentTypeId == componentTypeId)           
        .ToList();



遍历这些结果,并排除不必要的:

Iterate through the results and exclude the unwanted:

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;



完美的作品,如果不武。

Works perfectly, if not elegantly.

这篇关于LINQ查询返回过滤后的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆