This question already has answers here:
Natural Sort Order in C#
(18 answers)
Closed 6 years ago.
What's the easiest way to get a LINQ query (from an SQL database - does that matter?) to order strings naturally?
For example, I'm currently getting these results:
Project 1
Project 10
Project 2
What I'd like is to see is this:
Project 1
Project 2
Project 10
The query I'm using is this:
return from p in dataContext.Projects
orderby p.Name
select p;
There is no built-in way to do this using the .NET framework but I would suggest that you read Natural Sorting in C# for a discussion on the topic and an open-source implementation.
I'm only a few years late to the party, but I was just trying to solve a similar problem and this worked for me. Hope someone else finds this helpful.
Say you have your strings in a List, try something like this:
List<string> projects = new List<string>
{
"Project 1",
"Project 10",
"Project 2"
};
//Sort by a substring of the string value which omits the non-numeric characters
IEnumerable<string> sorted = projects.OrderBy(p => p.Substring(p.IndexOf(' ') + 2, p.Length - (p.IndexOf(' ') + 2)));
Related
This question already has an answer here:
Return the name of a function in lowercase
(1 answer)
Closed 3 years ago.
In my project I read a csv file. In this csv file there are different functions and their duration. Now I want to group my result by the function name but sometimes the name of the function is the same but the letters are sometimes upper and lower case like: examplevalue, EXampleValue.
In this example this would be 2 different functions.
To solve this problem I want to convert these functions into lower case. I tried like this but it is not working:
var descItemsTemp = db.ChartDatas
.GroupBy(x => new { x.Function.ToLower() });
You can pass a comparer to GroupBy, in this case you can use an existing:
.GroupBy(x =>x.Function, StringComparer.OrdinalIgnoreCase);
This question already has answers here:
LINQ to Entities - Addressing class properties with their string names
(3 answers)
Closed 4 years ago.
I'm using Entity Framework with C#. And I want to getting columns with string column name like below. How can I achieve this?
I want like below
context.Students.Select({
"Name",
"Surname",
"Number",
"BirthDate",
}).ToList();
Please don't advice below solution
context.Students.Select(p=> new {
p.Name,
p.Surname,
p.Number,
p.BirthDate,
}).ToList();
I don't think this is supported out of the box.
You can use context.Students.SqlQuery but then you will need to pass a complete SQL Statement.
Alternatively you may need to create some helper that maps string to properties, but that will involve reflection.
This question already has answers here:
How to get first N elements of a list in C#?
(7 answers)
Closed 4 years ago.
I have a list of objects in which each object has a property called "Frequency" and I want to be able to pick the top 10 objects that have the highest frequencies.
I saw some solutions that are kind of similar to what I am looking to solve using LINQ so any help is appreciated.
You can order the list by descending Frequency and then take the first 10 like this:
var top10 = objectList.OrderByDescending(o => o.Frequency).Take(10);
This question already has answers here:
Find character with most occurrences in string?
(12 answers)
Closed 7 years ago.
I'm trying to find a clever way using linq to examine an IEnumerable and find the max occurrences of some element.
"aba".SomeLinqExpression(); // => 'a'
or
(new List<int>{1, 2, 3, 4, 1, 2, 1}).SomLinqExpression(); // => 1
Is there an easy built in way to do this I think I need an aggregation query but Aggregate and Count don't seem to do it. Maybe groupby?
EDIT: Clarification
I'm looking to get access to the most often seen value. I don't care how ties are handled.
if we have a string
"abcda" // note that there are 2 'a' characters.
Since 'a' is the most common thing in the sequence I want this operation to return 'a'
"abcda".SomeLinqExpression(); // returns 'a'
Well, I guess this will work:
"aqejqkndaasaamd".GroupBy(x => x).OrderByDescending(g => g.Count()).First().Key
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
LINQ Between Operator
Dear All,
Hi,
I need to write this query in LINQ C#. can anyone help me?
Select *
From Mytable
where MyText BETWEEN 'john' AND 'Pear'
I believe this query should work:
var results = yourTable.Where(x => x.Text.CompareTo("john") > 0 &&
x.Text.CompareTo("Pear") < 0);
This assumes that you want to compare the text in each row of the table, and not some pre-dfined string.
Here is how you can do it with ObjectQuery
MytableSet.Where("it.Name between #start and #end", new ObjectParameter("start", "john"), new ObjectParameter("end", "Pear"))
EDIT:
Forget to mention that this statement is specific to Entity Framework not LINQ2SQL.