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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With