neo
neo

Reputation: 457

Custom ordering an Linq query over an observable collection

    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();

    }

How to order the above query by sequence of of customerIDs stored in a list li insted of default sorting orderby customerID.

Upvotes: 0

Views: 437

Answers (2)

Gregoire D.
Gregoire D.

Reputation: 445

I'm not sure of what you want, but if you want to list your data by CustomerID I guess, you can do something like :

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
    }
}

Upvotes: 1

YD1m
YD1m

Reputation: 5895

Check this:

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();

Upvotes: 1

Related Questions