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

Brian Leahy
  • 34,677
  • 12
  • 45
  • 60
ltech
  • 43
  • 1
  • 1
  • 4

6 Answers6

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();
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • 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 – ltech Feb 17 '10 at 19:39
  • 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). – tvanfosson Feb 17 '10 at 20:09
5

I think this solves problem:

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

denis.brulic
  • 51
  • 1
  • 3
  • 5
    Stack Overflow doesn't encourage code only answers, especially ones this late after the question was asked. Please add some commentary explaining your code. – slugster Oct 15 '12 at 09:44
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);
Amgad Fahmi
  • 4,349
  • 3
  • 19
  • 18
2

You could use IndexOf:

orderby ContactIds.IndexOf(a.CustomerId)

Note that this might not be efficient for large sets.

Timothy Macharia
  • 2,641
  • 1
  • 20
  • 27
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
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]  
    ...
Amy B
  • 108,202
  • 21
  • 135
  • 185
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
Jason Kleban
  • 20,024
  • 18
  • 75
  • 125