score:16
I'd like to recreate a generic version of the excel function AVERAGEIF
Why don't you just use LINQ?
var average = collection.Where(x => x.Something)
.Average(x => x.SomeProperty);
Note that this will throw an exception if there are no matching elements. If you don't want that, you could use:
var average = collection.Where(x => x.Something)
.DefaultIfEmpty()
.Average(x => x.SomeProperty);
It's not clear why you would want to create a separate method for this.
score:0
The following is the answer that works for me:
public double AVERAGEIF<T1,T2>(IEnumerable<T1> average_range, IEnumerable<T2> criteria_range, Func<T2, bool> criteria)
{
var t1andt2 = average_range.Zip(criteria_range, (l, r) => Tuple.Create<T1, T2>(l, r));
return t1andt2.Where(x => criteria(x.Item2)).Average(x => Convert.ToDouble(x.Item1));
}
void Main()
{
var average_range = new List<string>() {"10.1", "10.1", "0", "0"} ;
var criteria_range = new List<bool>() {true, true, false, false };
Func<bool, bool> criteria = c => c == true;
AVERAGEIF(average_range, criteria_range, criteria).Dump();
}
Source: stackoverflow.com
Related Articles
- How can I get the average of the elements of a generic list that meet a criteria?
- How to return a list of elements that were used in Average as well as their Average
- LINQ: Select Elements that Only Appear Once in a List
- How to remove elements of list of array/structs that have 2 common elements
- How do I use Linq to find the elements of a list that are not present in another list?
- Find objects in list that match elements in array
- Calculating the sum of the values in a dictionary that exist in a generic list
- Group to key-value pair where value is a list of elements that share the same key
- How to create a predicate that must meet two values with one being a list of values?
- LINQ for removing elements that are started with other element from list
- Linq for selecting elements from list 1 that exist on list 2 by comparsion between 2 properties values
- How to get the list elements that are not into another list C#
- Select objects in binding list that match criteria using LINQ?
- Sublists of consecutive elements that fit a condition in a list c# linq
- Use LINQ to select elements within a generic list and casting to their specific type
- LinQ query that counts the number of elements in a list of objects in another list of objects
- Remove List Elements that appear more than Once In Place
- How to find keys in a Dict<int,List<Tuple<string,string>>> such that the list contains elements with given Item1 and Items
- Get a count of elements that appear once in a list with LINQ
- c# Linq - Get all elements that contains a list of integers
- Linq to entities to return a list that contains elements from another list
- LINQ to identify a row that is the first in consecutive occurrences that meet criteria
- Find list of xml elements that have a list of names using linq (and remove them)
- Best/fastest way to select items from generic list that connect with offset mins
- LINQ query to return MAX date, where all days of that week meet a certain criteria
- Keep only elements of a list that have only letters
- How can I filter the elements in the list by certain criteria and then change their properties with Linq?
- Function that returns a Generic List
- Get elements of list that contains element of other list LINQ
- Average Certain Number Of Elements in List
- Linq Join Question
- "SELECT VALUE" - value keyword in LINQ/Entity Framework query
- LINQ: Not Any vs All Don't
- Compare two diferrent lists to Populate a third list
- Why does Lookup<TKey, TValue>.Grouping class not provide equality overrides?
- C# select duplicate using group
- Create generic array in VB.NET
- Linq many to many query with where clause
- Slow LINQ query against strongly typed Dataset
- Column referenced is not in scope: ''
- Linq Pattern Matching
- whats the issue with this LINQ query?
- MongoDB .Net Linq GroupBy Child Array AsQueryable
- c# linq filtering based on given list
- Parsing XSD w/XDocument
- Linq relational table include
- Join/Where with LINQ and Lambda
- Entity Framework 5 upgrade from 4
- LINQ to Entities does not recognize the method System.Guid
- Cannot convert lambda expression VB .net to C#