根据Linq中的DateTime过滤列表中的记录

时间:2017-06-04 11:30:13

标签: c# linq

以下是我的清单我希望找到基于datetime.ie的最近和交易最多的分支机构:在过去2个月内已经获得100笔存款的分行将首先与拥有约150个存款的银行相比较。最近5个月

     List<Branches> _branches = new List< Branches >
                {
                    new Branch
                    {
                        Id = 1,
                        Name = "Branch:1",
                        Deposits = new List<Deposit>(){
                        new Deposit{
                            Id=1,
                            DateOfDeposit="01/01/2016",
                            Amount=50
                        },
    ,
                        new Deposit{
                            Id=2,
                            DateOfDeposit="05/02/2017",
                            Price=30
                        },
                        new Deposit{
                            Id=3,
                            DateOfDeposit="01/01/2017",
                            Price=30
                        }
                    }
                    },continues...

1 个答案:

答案 0 :(得分:0)

您无法比较的是什么。您要求同时更改两个比较。您只能比较具有相同条件的项目。使用下面的代码可以帮助您入门。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
using System.Globalization;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Branch> branches = new List<Branch>() {
                new Branch() {
                    Id = 1,
                    Name = "Branch:1",
                    Deposits = new List<Deposit>(){
                        new Deposit{
                            Id=1,
                            DateOfDeposit = DateTime.ParseExact("01/01/2016","dd/MM/yyyy",CultureInfo.InvariantCulture),
                            Amount=50
                        },
                        new Deposit() {
                            Id=2,
                            DateOfDeposit = DateTime.ParseExact("05/02/2017","dd/MM/yyyy",CultureInfo.InvariantCulture),
                            Amount = 30
                        },
                        new Deposit() {
                            Id=3,
                            DateOfDeposit = DateTime.ParseExact("01/01/2017","dd/MM/yyyy",CultureInfo.InvariantCulture),
                            Amount = 30
                        }
                    }
                },
                new Branch() {
                    Id = 2,
                    Name = "Branch:2",
                    Deposits = new List<Deposit>() {
                        new Deposit{
                            Id=1,
                            DateOfDeposit = DateTime.ParseExact("01/01/2016","dd/MM/yyyy",CultureInfo.InvariantCulture),
                            Amount=50
                        },
                        new Deposit() {
                            Id=2,
                            DateOfDeposit = DateTime.ParseExact("05/02/2017","dd/MM/yyyy",CultureInfo.InvariantCulture),
                            Amount = 30
                        },
                        new Deposit() {
                            Id=3,
                            DateOfDeposit = DateTime.ParseExact("01/01/2017","dd/MM/yyyy",CultureInfo.InvariantCulture),
                            Amount = 30
                        }
                    }
                }
            };

            var results = branches.Select(x => new {
                Id = x.Id,
                month1Deposits = x.Deposits.Where(y => y.DateOfDeposit >= DateTime.Now.AddMonths(-1)).Count(),
                month2Deposits = x.Deposits.Where(y => y.DateOfDeposit >= DateTime.Now.AddMonths(-2)).Count(),
                month3Deposits = x.Deposits.Where(y => y.DateOfDeposit >= DateTime.Now.AddMonths(-3)).Count(),
                month4Deposits = x.Deposits.Where(y => y.DateOfDeposit >= DateTime.Now.AddMonths(-4)).Count(),
                month5Deposits = x.Deposits.Where(y => y.DateOfDeposit >= DateTime.Now.AddMonths(-5)).Count(),
                month6Deposits = x.Deposits.Where(y => y.DateOfDeposit >= DateTime.Now.AddMonths(-6)).Count(),
                month7Deposits = x.Deposits.Where(y => y.DateOfDeposit >= DateTime.Now.AddMonths(-7)).Count(),
                month8Deposits = x.Deposits.Where(y => y.DateOfDeposit >= DateTime.Now.AddMonths(-8)).Count(),
                month9Deposits = x.Deposits.Where(y => y.DateOfDeposit >= DateTime.Now.AddMonths(-8)).Count(),
                month10Deposits = x.Deposits.Where(y => y.DateOfDeposit >= DateTime.Now.AddMonths(-10)).Count(),
                month11Deposits = x.Deposits.Where(y => y.DateOfDeposit >= DateTime.Now.AddMonths(-11)).Count(),
                month12Deposits = x.Deposits.Where(y => y.DateOfDeposit >= DateTime.Now.AddMonths(-12)).Count()
            }).ToList();

        }
    }
    public class Branch
    {
        public int Id { get; set;}
        public string Name { get; set;}
        public List<Deposit> Deposits { get; set;}

    }
    public class Deposit
    {
        public int Id { get; set;}
        public DateTime  DateOfDeposit { get; set;}
        public int Amount { get; set;}
    }

}