使用LINQ从列表中的列表中进行选择 [英] Using LINQ to select from a List within a List

查看:86
本文介绍了使用LINQ从列表中的列表中进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我想把头缠在LINQ上,但是我发现自己在这里苦苦挣扎.我有几个非常简单的课程:

Hi!

I''m trying to wrap my head around LINQ, but I find myself struggling here. I have a couple of very simple classes:

class Order
    {
        public string AccountNumber;
        public string AccountName;
        public List<OrderLine> OrderLines = new List<OrderLine>();
        public DateTime StartDate;
        public DateTime EndDate;       
    }

    class OrderLine
    {
        public string Description;
        public string ProductCode;
        public double Duration;
        public int Quantity;
    }



在运行时,我用Orders实例填充了一个名为customerOrders的新List对象,该对象又具有多个OrderLine实例.现在,我想做的是选择在整个customerOrder对象中使用的独特ProductCode.我已经尝试了一下,并尝试了要塞,这是到目前为止我要提出的内容:



At run-time, I populate a new List object called customerOrders with instances of Orders, which again have multiple instances of OrderLine. Now, what I would like to do is to select the distinct ProductCode used in the entire customerOrder object. I have tried back and fort abit, and this is what I''ve come up with so far:

var c = from d in CustomerOrders select d.OrderLines.ToList();
IEnumerable<string> b = from q in (c as List<OrderLine>) select q.ProductCode;


第一行很好,但是下一行会导致异常,因为c为null.
我什至没有尝试从列表中查找不同的值:)

帮助和提示大加赞赏!

/Geir Rune


The first line is fine, but the next one causes an exception becase c is null.
I didn''t even get to trying to find the distinct values from the list :)

Help and tips much appreciated!

/Geir Rune

推荐答案

如果要扁平化"可枚举的枚举,则SelectMany扩展方法可以解决问题.
If you want to "flatten" enumerable of enumerables, the SelectMany extension method will do the trick.

var orders = new List<Order>();
var distinctOrderLines = orders.SelectMany(order => order.OrderLines).Distinct();


朋友,

据我了解,.. Iam为您的问题提供了一个小型且最佳的解决方案

下面的QueryExpression可以解决您的问题

//第一步:从订单中获取订单行
var OrderLines =从objOrder.OrderLines.ToList()中的pc1开始 选择pc1;

//step2:获取不同的产品代码
var productCodes =(来自OrderLines中的p1
选择新的{p1.ProductCode}).Distinct();

//Step3:将不同的产品代码绑定到GridView
gvOrderLines.DataSource = productCodes;
gvOrderLines.DataBind();





希望以上代码对您有所帮助.
祝一切顺利...编码愉快.

最好的问候,
Venkatesh velpula.
Hi Friend,

According to my knowledge..here Iam giving a small and best solution for your problem

The below QueryExpression can solve your problem

//Step1: Get the OrderLines from Orders
var OrderLines = from pc1 in objOrder.OrderLines.ToList()
select pc1;

//step2: Get the Distinct ProductCodes
var productCodes = (from p1 in OrderLines
select new { p1.ProductCode }).Distinct();

//Step3: Bind the Dictinct ProductCodes to GridView
gvOrderLines.DataSource = productCodes;
gvOrderLines.DataBind();





I hope above code will help you.
All the Best...Happy Coding.

Best Regards,
Venkatesh velpula.


您是否尝试过在没有"as"的情况下进行操作?我认为var的全部要点是您不需要指定类型吗?如果使用调试器,则c的类型是什么?
Have you tried doing it without the ''as'' ? The whole point of var, I thought, was that you don''t need to specify the type ? If you use the debugger, what IS the type of c ?


这篇关于使用LINQ从列表中的列表中进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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