score:11
what about
var list = new list<int>{1,1,0,0,0};
var percentage = ((double)list.sum())/list.count*100;
or if you want to get the percentage of a specific element
var percentage = ((double)list.count(i=>i==1))/list.count*100;
edit
note brokenglass's solution and use the average
extension method for the first case as in
var percentage = list.average() * 100;
score:0
if you
- want to do it in one line
- don't want to maintain an extension method
- can't take advantage of
list.sum()
because your list data isn't 1s and 0s
you can do something like this:
percentachieved = (int)
((double)(from myclass myclass
in mylist
where myclass.someproperty == "somevalue"
select myclass).tolist().count /
(double)mylist.count *
100.0
);
score:1
best way to do it:
var percentage = ((double)list.count(i=>i==1))/list.count*100;
or
var percentage = ((double)list.count(i=>i <= yourvaluehere))/list.count*100;
score:5
if you're working with any icollection<t>
(such as list<t>
) the count
property will probably be o(1); but in the more general case of any sequence the count()
extension method is going to be o(n), making it less than ideal. thus for the most general case you might consider something like this which counts elements matching a specified predicate and all elements in one go:
public static double percent<t>(this ienumerable<t> source, func<t, bool> predicate)
{
int total = 0;
int count = 0;
foreach (t item in source)
{
++count;
if (predicate(item))
{
total += 1;
}
}
return (100.0 * total) / count;
}
then you'd just do:
var list = new list<int> { 1, 1, 0, 0, 0 };
double percent = list.percent(i => i == 1);
output:
40
score:6
in this special case you can also use average()
:
var list = new list<int> {1,1,0,0,0};
double percent = list.average() * 100;
Source: stackoverflow.com
Related Query
- What is the best way in linq to calculate the percentage from a list?
- What is the best way to get the percentage for an object in linq list and map it to JSON?
- What is the best way to create strongly typed LINQ queries from some given strings, via reflection
- What is the correct way of retrieving a given business object from a list of business objects using LINQ Where vs. Find?
- What is the best way to find length of split characters from the given string by using String.Split() Method or Linq Lambda Expression in C#
- What is the best way to cast each item in a LINQ to Entities query to an interface?
- What is the best way to separate string using string.format() function or LINQ ?
- What is the return type for a anonymous linq query select? What is the best way to send this data back?
- What is the best way to get the level from a tree data structure using LINQ?
- What is the correct way to get a sortable string from a DateTime in a LINQ entity?
- What is the best way to optimize or "tune" LINQ expressions?
- What is the fastest way to compare a value from a list to a specific sum from another list?
- Best way to join 3 lists and count how many times an ID is in the list using linq
- In C#, What is the best way to see if a list contains another list?
- What is the best way to sort a List of objects with a start value?
- what is the most elegant way to update an item in one list from another list in C#?
- Best LINQ way to create dictionary from list of custom entities
- What is the best way to order a list by numeric field and put objects with value of 0 at the bottom?
- What's the best way to copy and insert data from related tables into a new set of related tables with Linq
- What is the best way to write a two column query in LINQ to Entity 6? And save the results to two different variables?
- Bind a Linq Lambda Select Many from Controller to view in MVC4? What are the Best ways to return in view
- What is the best way to test a condition in Linq to NHibernate?
- In C#, what is the best way to sort a list of objects by a string property and get correct order?
- Better way calculate percentage from class properties using LINQ
- What is the best way in LINQ to sort 1000000 Records
- LINQ get the best match from the list comparing strings
- Linq code to get the index of an object in an array from an object within a list
- What is the best way in assigning foreign key when using entity framework & LINQ to Entities?
- way to go from list of objects to SortedList<string,string> with two of the fields using LINQ
- What is the best way to perform a left/right outer join in LINQ and Entity Framework (method)?
More Query from same tag
- Display List of Objects in a group with self referencing foreign key
- "Value cannot be null" while creating an XElement from the attributes where one or more value is null
- How to query a child object
- Linq Converting String to List
- Model Binding DBContext disposed on page load
- Removing values from a generic list
- EF LINQ include multiple and nested entities
- With LINQ how do you return a string const in the result from the select operator
- How to write LINQ IN clause query which will work as LIKE operator as well?
- LINQ Query Problem
- How to order enum alphabetically rather than on int?
- Conversion from System.Array to List<string> using LINQ, need to keep empty values
- Can I have an Any in a linq query?
- Using 'OR' inside a LinQ join query (adapting SQL into LinQ)
- .SelectMany with C#
- LINQ Group by & SUM to a LONG variable
- Linq Joins only working when all tables have data
- Foreign Key Mapping
- IN query using LINQ for IEnumerable List of items
- how to turn nested foreach loop with C# linq
- LINQ multiple childs in 1 row
- Lazy loading repeater has linq query not yielding results
- Why can't I call an extension method from a base class of the extended type?
- Do not nest generic types in member signatures
- Can i use Linq to iterate/filter my web.config AppSettings?
- LINQ GroupBy result of previous GroupBy
- Order List By Distinct And Object Property Larget value
- EF Core include lists only if a property is true
- How to write such a query SELECT * FROM TABLE WHERE COLUMN LIKE '%"BLABLA":[%"BLABLA1"%]%' using Entity Framework
- Lazily partition sequence with LINQ