使用LINQ在ReportDataSource中检索嵌套List

时间:2014-09-11 17:16:41

标签: c# linq reporting-services

所以这就是我的问题。我有一个使用子报表的ms报告(.rdlc)。我的数据模型是:

public class Customer
{
    public Customer(int id)
    {
        Id = id;
        Orders = new List<CustomerOrder>();
    }

    public Customer(int id, string name, string address, string city, string state, IEnumerable<CustomerOrder> orders ):this(id)
    {
        Id = id;
        Name = name;
        Address = address;
        City = city;
        State = state;
        Orders.AddRange(orders);
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public List<CustomerOrder> Orders { get; set; }
}

public class CustomerOrder
{
    public CustomerOrder()
    {
    }

    public CustomerOrder(int id, string salesOrderNumber, string orderDate, string total)
    {
        Id = id;
        SalesOrderNumber = salesOrderNumber;
        OrderDate = orderDate;
        Total = total;
    }

    public int Id { get; set; }
    public string SalesOrderNumber { get; set; }
    public string OrderDate { get; set; }
    public string Total { get; set; }
}

有一个事件要挂钩子报表。它的使用方式如下:

 LocalReport localReport = new LocalReport();

...

 localReport.SubreportProcessing += localReport_SubreportProcessing;

...

 void localReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
 {

    var mainSource = ((LocalReport)sender).DataSources["dsCustomers"];
    var subSource = ((List<Customer>)(mainSource.Value)).SelectMany(t => t.Orders).ToList();

    e.DataSources.Add(new ReportDataSource(SubReportDSName, subSource));

}

我试图根据Id值恢复List。当我运行此操作时,我会将所有订单列入清单,以获取存在的尽可能多的客户ID。如果我在主报告中当然有四个客户,那么这个事件会被击中四次。所以我需要一种方法来使用LINQ过滤LIST并且不确定正确的方法。这是一个数据样本。

    private List<Customer> customers = new List<Customer>();
    private List<CustomerOrder> custOrders = new List<CustomerOrder>(); 

    /// <summary>
    /// 
    /// </summary>
    public CustomersRepository()
    {

        try
        {
            custOrders.Add(new CustomerOrder { Id = 1, OrderDate = "12/14/2003", SalesOrderNumber = "SO59986", Total = "2372.95" });
            custOrders.Add(new CustomerOrder { Id = 1, OrderDate = "12/27/2003", SalesOrderNumber = "SO60850", Total = "2443.35" });
            custOrders.Add(new CustomerOrder { Id = 1, OrderDate = "04/08/2002", SalesOrderNumber = "SO45861", Total = "3578.27" });
            custOrders.Add(new CustomerOrder { Id = 1, OrderDate = "12/22/2003", SalesOrderNumber = "SO60561", Total = "2419.06" });

            customers.Add(new Customer(1, "Aaron Wright", "7024 rue Surcouf", "Bobigny", "Seine Saint Denis", custOrders));

            custOrders.Clear();

            custOrders.Add(new CustomerOrder { Id = 2, OrderDate = "08/08/2003", SalesOrderNumber = "SO52261", Total = "42.28" });
            custOrders.Add(new CustomerOrder { Id = 2, OrderDate = "12/19/2003", SalesOrderNumber = "SO60342", Total = "195.59" });
            custOrders.Add(new CustomerOrder { Id = 2, OrderDate = "02/15/2004", SalesOrderNumber = "SO64201", Total = "17.27" });
            custOrders.Add(new CustomerOrder { Id = 2, OrderDate = "06/19/2004", SalesOrderNumber = "SO73274", Total = "28.99" });

            customers.Add(new Customer(2, "Alexandria Bell", "8346 Niagara Court", "Langford", "British Columbia", custOrders));

            custOrders.Clear();

            custOrders.Add(new CustomerOrder { Id = 3, OrderDate = "12/13/2003", SalesOrderNumber = "SO59957", Total = "2319.99" });
            custOrders.Add(new CustomerOrder { Id = 3, OrderDate = "09/03/2002", SalesOrderNumber = "SO47472", Total = "2181.5625" });

            customers.Add(new Customer(3, "Alexandria Bell", "6275 Bel Air Drive", "Redmond", "Washington", custOrders));

            custOrders.Clear();

            custOrders.Add(new CustomerOrder { Id = 4, OrderDate = "03/19/2004", SalesOrderNumber = "SO66442", Total = "2354.98" });
            custOrders.Add(new CustomerOrder { Id = 4, OrderDate = "09/30/2001", SalesOrderNumber = "SO44271", Total = "3578.27" });

            customers.Add(new Customer(4, "Lisa Zheng", "8360 Frisbie Court", "Newcastle upon Tyne", "England", custOrders));
        }

我试图添加一个int Indx;在被调用的方法中并使用where条件:

var subSource = ((List<Customer>)(mainSource.Value)).SelectMany(t => t.Orders).ToList();

然后是++ Indx,但是这很难失败,因为Indx第二次或第三次接近方法调用就像是12或15等等。所以这必须是由于使用Microsoft报表呈现引擎进行线程化。

报告中的预期产出:

客户#1姓名,地址 - &GT;子报表:     订单#1 OrderDate SalesOrderNumber总计              OrderDate SalesOrderNumber总计

客户#2姓名,地址 - &GT;子报表:     订单#2 OrderDate SalesOrderNumber总计              OrderDate SalesOrderNumber总计

任何想法,只需要使用LINQ正确提取我需要的订单列表,我们将不胜感激。

谢谢, d

好的,所以主要的问题是它与报告无关,而是关于LINQ。该声明为我提供了CustomerOrders的通用列表:

 var subSource = ((List<Customer>)(mainSource.Value)).SelectMany(t => t.Orders).ToList();

什么是正确的where子句过滤使用linq来恢复此列表的某个子集?

0 个答案:

没有答案