Accepted answer

You can sort by the index in the input array after using AsEnumerable():

var input = "2207,117,90,2168,120,118,113,112,17".Split(',');
var recordIds = Array.ConvertAll(input, int.Parse);

var styles = db.DataModel
                .Where(c => c.CategoryTypeID == 5 && c.Enabled && recordIds.Contains(c.ID))
                .OrderBy(c => Array.IndexOf(recordIds, c.ID))

Above assumes you want to load all Category objects that have an ID that is contained in recordIds. Once they are retrieved you order them by their index in the array.


You can use the comma-separated list filter hack:

...Where(c => ("," + myString + ",").IndexOf("," + c.CatgeoryID + ",") != -1)...

Using a similar technique, you can get them back in the same order:

...OrderBy (c => ("," + myString + ",").IndexOf("," + c.CategoryID + ","))...


If you don't want to use Linq-to-objects you can do the following:

int[] ids =  "2207,117,90,2168,120,118,113,112,17".Split(',').Select(x => int.Parse(x)).ToArray();

var styles = from c in db.DataModel.Categories
             join i in ids on c.ID equals i
             where c.CategoryTypeID == 5 && c.Enabled
             select x;

Related Articles