Linq列表的列表数之和

本文关键字:列表 Linq | 更新日期: 2023-09-27 18:09:28

我有一个这样的代码(我已经简化了实际情况)

public class A
{
    public List<B> list { get; set; }
}
public class B
{
    public List<C> list{ get; set; }
}
public class C
{
    public DateTime Date { get; set; }
    public int num { get; set; }
}

我想求和日期是今天的num值。但总有一个错误因为我有一个列表(在B类中)在列表(在a类中)我该怎么办?我试着

        A a = new A();
        B b1 = new B();
        C C1 = new C() { Date = DateTime.Today, num = 2 };
        C C2 = new C() { Date = DateTime.Today.AddDays(1), num = 3 };
        b1.list.Add(C1);
        b1.list.Add(C2);
        a.list.Add(b1);
        B b2 = new B();
        C C3 = new C() { Date = DateTime.Today, num = 4 };
        C C4 = new C() { Date = DateTime.Today.AddDays(1), num = 5 };
        b2.list.Add(C3);
        b2.list.Add(C4);
        a.list.Add(b2);
        var tot = (from l in a.list
                   where l.list.Where(x => x.Date == DateTime.Today)
                   select l;

Linq列表的列表数之和

您可以使用外部列表中的SelectMany来"平化"内部列表,如下所示:

var tot = a.list.SelectMany(bItem => bItem.list)
    // From this point on, you see a "flat" IEnumerable<C>
    .Where(cItem => cItem.Date == DateTime.Today)
    .Sum(cItem => cItem.num);

您的类已初始化存储:

public class A
    {
        public A()
        {
            this.list = new List<B>();
        }
        public List<B> list { get; set; }
    }
    public class B
    {
        public B()
        {
            this.list = new List<C>();
        }
        public List<C> list { get; set; }
    }
    public class C
    {
        public DateTime Date { get; set; }
        public int num { get; set; }
    }

如果你修复它,你可以使用SelectMany方法。

        var tot = a
            .list
            .SelectMany(bItem => bItem.list)
            .Where(cItem => cItem.Date == DateTime.Today)
            .Sum();

对于标准队列,你可以这样做

 var totLinq = (from bItem in a.list
               from cItem in bItem.list
               where cItem.Date == DateTime.Today
               select cItem.num).Sum();

请检查下面的代码:

public class A
{
    public List<B> list { get; set; }
    public  A(){
        this.list  = new List<B>();
    }
}
public class B
{
    public List<C> list{ get; set; }
    public  B(){
        this.list  = new List<C>();
    }
}
public class C
{
    public DateTime Date { get; set; }
    public int num { get; set; }
}
class Program
{
    static void Main()
    {
        A a = new A();
        B b1 = new B();
        C C1 = new C() { Date = DateTime.Today, num = 2 };
        C C2 = new C() { Date = DateTime.Today.AddDays(1), num = 3 };
        b1.list.Add(C1);
        b1.list.Add(C2);
        a.list.Add(b1);
        B b2 = new B();
        C C3 = new C() { Date = DateTime.Today, num = 4 };
        C C4 = new C() { Date = DateTime.Today.AddDays(1), num = 5 };
        b2.list.Add(C3);
        b2.list.Add(C4);
        a.list.Add(b2);
        var tot = (from bItem in a.list
                   from cItem in bItem.list
                   where cItem.Date == DateTime.Today
                   select cItem);
    }
}

解决方案

    static void Main(string[] args)
    {
        A a = new A();
        B b1 = new B();
        C C1 = new C() { Date = DateTime.Today, num = 2 };
        C C2 = new C() { Date = DateTime.Today.AddDays(1), num = 3 };
        b1.list.Add(C1);
        b1.list.Add(C2);
        a.list.Add(b1);
        B b2 = new B();
        C C3 = new C() { Date = DateTime.Today, num = 4 };
        C C4 = new C() { Date = DateTime.Today.AddDays(1), num = 5 };
        b2.list.Add(C3);
        b2.list.Add(C4);
        a.list.Add(b2);
        //we took advantage of the SelectMany extension method.
        var cResuls = a.list.SelectMany(x => x.list).Where(x=> x.Date==DateTime.Today).Sum(x=> x.num);
        Console.WriteLine("Sum of Today Value : {0} ", cResuls.ToString());
        Console.ReadLine();
    }
    public class A
    {
        public List<B> list { get; set; }
         public A()
        {
            list = new List<B>();
        }
    }
    public class B
    {
        public List<C> list { get; set; }
        public B()
        {
            list = new List<C>();
        }
    }
    public class C
    {
        public DateTime Date { get; set; }
        public int num { get; set; }
    }