score:3
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
.Categories
.Where(c => c.CategoryTypeID == 5 && c.Enabled && recordIds.Contains(c.ID))
.AsEnumerable()
.OrderBy(c => Array.IndexOf(recordIds, c.ID))
.ToList();
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.
score:0
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 + ","))...
score:0
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;
Source: stackoverflow.com
Related Articles
- Load Records From EF Data Model in Custom/Predefined Order Using LINQ
- Details' View with related data via LINQ using a custom model
- How to reinsert data from one table onto itself using LINQ in code migration?
- Load data from database to richtextbox in c# windows form using Linq
- How to convert the return type of the model data from Product entity to ProductViewModel objects using LINQ select?
- c# Linq or code to extract groups from a single list of source data
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- Data paging in linq without removing records from the data source
- How to eager load sibling data using LINQ to SQL?
- Using Linq on a Client Object model result from sharepoint
- Retrieving Data from database within the last 7 days using linq
- Extract data from a XML string using linq vs xmlDocument
- Using LINQ to delete an element from a ObservableCollection Source
- How does linq actually execute the code to retrieve data from the data source?
- LINQ - C# - Using lambda - Get a set of data from a Collection
- Start reading data from a specific row of excel using Linq in C#
- Delete all records from a database using LINQ to SQL
- Eliminate comma(,) from a column of a Data Table using LINQ
- Display data in hierarchical order using LINQ
- Read text data from file using LINQ
- using linq select from list data where
- Using a method vs. a property to retrieve data from a class using LINQ
- Conditional average using linq from qrouped data returns nothing
- Is a full list returned first and then filtered when using linq to sql to filter data from a database or just the filtered list?
- creating Linq to sqlite dbml from DbLinq source code
- Fetch data From Two tables using Linq
- Pull data from multiple tables in one SQL query using LINQ and Entity Framework (Core)
- get parent name and child count from model using LINQ
- read icollection data using LINQ in C# code
- Import Data from Excel to SQL Server DB using LINQ to Excel
- How to work inline with custom IEqualityComparer<T> parameters
- Retrieving xml parts uing Linq to XML
- How To Get All Tweets on Hashtag using LinqToTwitter
- Linq join that accepts null values
- How to Check or Uncheck a Checkbox in Datagridview based on existing data record from Table
- Can I use the orderby linq keyword with a comparer?
- What is the difference between Converting and Unboxing?
- how to run linq on XxmlElement rather than XElement in C#
- LINQ join query with relational entities
- using linq-like expression in angular ng-show
- Get chained property names from MemberExpression
- Linq Group by and get all values for a specific string rule returning all columns
- Min tuple in IGrouping of tuples
- Don't order by attribute
- LINQ Union - string keep orderby
- LINQ Entity Framework sequence contains no elements
- Request values from query string
- Selecting only unique values from collection
- Query entry with distinct source?
- LINQ to SQL, condition on foreign entities