How to group by ToLower()? [duplicate] - c#

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);

Related

Use a Like expression in Lambda [duplicate]

This question already has answers here:
Case insensitive 'Contains(string)'
(29 answers)
Closed 4 years ago.
I have a Lambda expression that search the column after submitting a form.
It does work when I submit it but its doesn't search the right way I would like to search.
I would like to make it work the same way it search in SQL like statement.
select * FROM tableSearch where subject like '%f5%'
This way even if 'F' is capital it still finds it.
Can this be possible using Lambda expression.
With the below code it only finds it if 'F' is not capital unless i enter 'F5' in subject.
if (!string.IsNullOrEmpty(searchControl.subject))
{
searchList = searchList.Where(x => x.subject.Contains(searchControl.subject)).ToList();
}
why not make them both ToLower:
searchList.Where(x => x.subject.ToLower().Contains(searchControl.subject.ToLower()))
or:
searchList.Where(x => x.subject.IndexOf(searchControl.subject, StringComparison.OrdinalIgnoreCase) >= 0)
You need to use StringComparison as below:
searchList = searchList.Where(x => x.subject.Contains(searchControl.subject,StringComparison.CurrentCultureIgnoreCase)).ToList();
By using that you ignore case sensitivity.

Linq - How can I run each item through a function and then pick the item with the highest function result [duplicate]

This question already has answers here:
How to perform .Max() on a property of all objects in a collection and return the object with maximum value [duplicate]
(9 answers)
Closed 5 years ago.
What I want to do is basically
var max = things.Select(t => ExpensiveFunc(t)).Max();
var ThingWithMaxResult = things.Where(t => ExpensiveFunc(t) == max).First();
But I don't want to have to run the ExpensiveFunc twice on each element.
I am learning LINQ so I would like to know the LINQ way of doing this. Otherwise I would normally create an array of things and results, then just pick the array with the highest result.
You can order by the function call (descending) and take the first one:
var max = things.OrderByDescending(ExpensiveFunc).First();

counting occurances of item in IEnumerable using linq in C# [duplicate]

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

Query a list of objects that contains any string [duplicate]

This question already has answers here:
Check if a string contains an element from a list (of strings)
(12 answers)
Closed 8 years ago.
I'm this expression to search inside a list of objects by a specific property:
var result = myObject.Where(o => o.SearchString.Contains(searchValue));
It works good for a single value. The searchValue is a string passed by the user. The user can pass a single word or many words separeted by spaces. Is there any way to filter the objects that contains any of the passed words?
I could do this with a loop, searching a new word in previous results, but it doesn't seem very elegant.
myObject.Where(o => words.Any(o.SearchString.Contains))

Get distinct parameters from a List [duplicate]

This question already has answers here:
Get distinct items from a list
(4 answers)
Linq Distinct() by name for populate a dropdown list with name and value
(7 answers)
LINQ: Distinct values
(8 answers)
Closed 9 years ago.
I have a CarData object with the following properties:
PrimaryKey Make Model Year Drivetrain Country
I have about 1000 of these CarData objects in a List :
List<CarData> CarObjects
Is there a simple way to get a list of the distinct Makes?
var makes = CarObjects.Select(car => car.Make).Distinct();
This transforms the list from a list of CarData to a list of Makes, and then just finds the distinct values of the new list.
You can use Linq:
CarObjects.Select ( c => c.Make ).Distinct().ToList()
var makeList = CarObjects.Select(a => a.Make).Distinct();
Or
List<MakeEnum> = CarObjects.Select(a => a.Make).Distinct().ToList();
As an extra bit of advice, you may want to consider having Make be an enum, since there are (presumably) a finite (and rather small) number of possible makes of cars, instead of piling them into Strings. (You don't make a mention of what kind of property Make is, so maybe you are already doing this).

Categories