4

Say I have a method definition as such:

public CustomerOrderData[] GetCustomerOrderData(string[] CustomerIDs)
{
 var query = (from a in db.Customer
              join b in db.Order on a.CustomerID equals v.CustomerID
              orderby CustomerIDs
              select new CustomerOrderData()
              {
                //populate props here
              }).ToArray();
}

My CustomerIDs in input param could be {"1","3","400","200"}

I want my return array to be ordered in the above fashion. Is there an easy way to achive this?

My solution was to put it into a Dictionary and then create a new array while looping through my CustomerIDs collection.

CustomerOrderData does have a property named CustomerID

6 Answers 6

7

If you materialize the query, you should be able to find the index of the id in your array and use it as the ordering parameter. Shown below using extension methods.

var ordering = CustomerIDs.ToList();
var query = db.Customer.Join( db.Order, (a,b) => a.CustomerID == b.CustomerID )
                       .AsEnumerable()
                       .OrderBy( j => ordering.IndexOf( j.Customer.CustomerID ) )
                       .Select( j => new CustomerOrderData {
                          // do selection
                        })
                       .ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

tvanfosson: I tried your method, the returned array does not appear to be sroted by my CustomerIDs string[]. Seems that data is sorted in descending order
Is customerID a string in the entity class? If not, try doing an IndexOf( j.Customer.CustomerID.ToString() ) -- or change your array to an array of int values (assuming that's the type on the entity).
5

I think this solves problem:

var orderedArray = YourCustomArray.OrderBy(s => s).ToArray();

1 Comment

Stack Overflow doesn't encourage code only answers, especially ones this late after the question was asked. Please add some commentary explaining your code.
4

if the customerIds always will be numbers then cast it and order it before using it into ur query

var orderedIds =  CustomerIDs.Cast<int>().OrderBy(x => x);

Comments

2

You could use IndexOf:

orderby ContactIds.IndexOf(a.CustomerId)

Note that this might not be efficient for large sets.

Comments

0

You could create a structure to lookup the desired index using the customerid.

string[] CustomerIDs;

Dictionary<string, int> customerIDOrdering = CustomerIds
  .Select((c, i) => new (id = c.CustomerID, i = i})
  .ToDictionary(x => x.id, x => x.i);

var query = from c in ...
    orderby customerIDOrdering[c.CustomerID]  
    ...

Comments

0

The join clause preserves the order of the outer sequence:

"The Join operator preserves the order of the outer sequence elements, and for each outer element, the order of the matching inner sequence elements."

http://msdn.microsoft.com/en-us/library/bb394939.aspx

So you should not need to orderby at all:

from orderIndex in CustomerIDs
join a in db.Customer on orderIndex equals a.CustomerID
join b in db.Order on a.CustomerID equals v.CustomerID

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.