This question already has answers here:
IEnumerable doesn't have a Count method
(5 answers)
Closed 4 years ago.
I have an observable collection and I'm trying to count the number of objects where "IsActive" = true. This looks like it should work, but I get an error saying "Count can't be used as a method". Anyone know how to do this?
int count = createAndDisplayViewModel.AvailableMonitorsForAddOC.Count(p => p.IsActive);
You are missing using System.Linq in the top of the file.
The misleading error message ("Count can't be used as a method") is given because the collection has a Count property, and it does not know about the extension method.
Related
This question already has answers here:
FindAll vs Where extension-method
(5 answers)
Slow LINQ query for .ToArray()
(3 answers)
C# FindAll VS Where Speed
(5 answers)
Closed 3 years ago.
I have an array of structs and I need to locate all the elements that share a certain condition.
Currently I use:
Array.FindAll(someArray, x => x.eg == "*Perfomance Test*")
But after some googling I noticed that there is a much faster way by using the LINQ Where-method.
someArray.Where(x => x.eg == "*Perfomance Test*")
I did some testing and the results are pretty impressive:
FindAll: 00:00:03.06
Where: 00:00:00.20
The problem is that Where returns IEnumerable. And I call this method that returns all these certain elements from the array within the for loop. Which is the type of loop I need to use. I used the .ToArray() method, but that made it much worse so that FindAll() is the faster approach.
Because of that it seems to me that if I need to get an array of elements, Where is much slower option than FindAll. But maybe I'm missing something.
Are there any better, faster options?
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:
Is using Random and OrderBy a good shuffle algorithm? [closed]
(13 answers)
Are GUIDs timely ordered ? If ORDER BY used with a GUID variable type, will records created lately come late?
(3 answers)
Closed 5 years ago.
Which one is faster?
Im doing tests on a ConcurrentDictionary and want to randomize it and just return 1 result. Essentially pick a random result from it.
Which method is faster/more efficient?
Efficiency as in, as little cpu/memory while having low possible errors like conflicts and such.
Method A.
Random rand = new Random();
var result = concDict.ElementAt(rand.Next(concDict.Count() - 1));
Method B.
var result = concDict.OrderBy(x=>Guid.NewGuid()).First();
In my "vague" testing I dont see much difference apart from Method B being more efficient. Method A can succum to the concDict's Count being out of sync with the concDict.ElementAt causing ArgumentOutOfRangeException: 'maxValue' must be greater than zero. while Method B literally cant cause that.
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();
This question already has answers here:
LINQ's Distinct() on a particular property
(23 answers)
Closed 8 years ago.
Distinct() is not working. It displays all the repeating values.
I searched for a solution but just got more confused. I tried this :
var categories = db.Orders.OrderBy(c => c.Item1).ToList().Distinct();
var categories = db.Orders.Distinct().OrderBy(c => c.Item1).ToList();
Is there a quick uncomplicated way to make this work?
use GroupBy instead of Distinct