自定义对可观察集合的Linq查询

时间:2013-07-04 13:33:30

标签: c# linq entity-framework observablecollection wpf-4.0

    public PrintCustomerAddress(PrintDocument doc, SEIKISHIRYOEntities context, ObservableCollection<PrintCustomerItem> customerForPrintPage)
    {

        this.context = context;
        this.customerForPrintPage = customerForPrintPage;

        printCustomerList = (from o in customerForPrintPage
                             select o).ToList();
        li = (from o in customerForPrintPage
              select o.CustomerID).ToList();
        //List<int> orderLst=getCustomerID(li);

        CustomerData = (from data in context.CustomerCompanyTables
                        where (from o in li select o).Contains(data.CustomerID)
                        orderby data.CompanyName ascending
                        select new CustomerCompanyTableMap
                        {
                            CompanyName = data.CompanyName,
                            CustomerID = data.CustomerID

                        }).ToList();

    }

如何按照客户ID默认排序顺序列表中存储的customerID序列来订购上述查询。

2 个答案:

答案 0 :(得分:1)

检查一下:

CustomerData = (from _li in li
                join data in context.CustomerCompanyTables on _li equals data.CustomerID
                select new CustomerCompanyTableMap
                {
                    CompanyName = data.CompanyName,
                    CustomerID = data.CustomerID
                }).ToList();

答案 1 :(得分:1)

我不确定你想要什么,但如果你想通过CustomerID列出你的数据,我猜你可以这样做:

var CustomerData = from data in context.CustomerCompanyTables
                where li.Contains(data.CustomerID)
                group data by data.CustomerID into result
                select result;

foreach (var customer in CustomerData)
{
    var customerID = customer.Key;

    foreach (var data in customer)
    {
        // do stuff
    }
}