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();
}
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.
Source: stackoverflow.com
Related Query
- How can I get the average of the elements of a generic list that meet a criteria?
- How can I get the average of a property in a list using LINQ?
- How to get the list elements that are not into another list C#
- How can I get the next appropriate value from a generic list using LINQ?
- How can I filter the elements in the list by certain criteria and then change their properties with Linq?
- How can I retrieve an object from a generic list based on the value of two of that object's members?
- How can I get the index of an item in a list in a single step?
- How to get all elements except the n'th element in a List using Linq
- How do I use Linq to find the elements of a list that are not present in another list?
- How can I speed up this linq query on a List<> with search criteria on 3 attributes in the list object
- How to get list that is the distinct select of other lists (LINQ)?
- How can I get the the half of elements of a list?
- Using ASP.NET and MVC 3, how can I create hidden fields so that a List with an array as a value of each item in the list binds correctly?
- Using Linq to object, how can I get one field's value based on another in the same list
- How to find keys in a Dict<int,List<Tuple<string,string>>> such that the list contains elements with given Item1 and Items
- How can I make a list of the element numbers in a list that have a field that is not null?
- I have 2 Lists of strings. How do I get a bool that tells me if one lists contains atleast one string from the other list ? (Using Lambda)
- How to get the property from a list of object that is a string with the lowest numeric value
- How can I get a list of all users in groups linked to the admin?
- How can I filter a list to exclude elements where the InnerText property does not contain "See {"
- How to return a list of elements that were used in Average as well as their Average
- How to get records if only all of the list elements included in nested collections?
- how i can get the item Of subGroup list Through the group List
- Using Linq to XML, how can I select the elements that have exactly x number of parent elements?
- How can I search a list of objects that contains a list of objects for the first entry that contains an element matching my search criteria?
- How can I Using LinQ search in a list by the input and print those that contain the letters?
- How can I take a list of dates, and remove any that are within 1 minute of another date in the list with LINQ?
- How can I get the last 50 elements in a list?
- How can I get a list of DirectoryInfo where the name of the directories contains a string stored in a List<string>?
- How can I get the average of two different columns in linq?
More Query from same tag
- Linq select one occurrence
- Dotnet EF Core Linq string contains in a list string split by comma
- Document Number Evaluated Improperly Due to String Compare
- Possible InvalidOperationException when working with Nullable DateTime in LINQ
- Dynamically generate LINQ
- ObjectDisposedException after returning from LINQ to SQL query
- Paging with LINQ for objects
- Entity Framework method group Include does not work
- LINQ to XML query not working
- sorting list in a list
- Querying one record of each type between dates
- C# LINQ and XML Parsing with Separate Sections
- Datatable server side processing c# mvc
- how to copy one column data to another column with in the same table using linq?
- In LINQ expression how to get FirstOrDefault with better performance?
- How to sort a list of objects by a property of a nested list with Linq?
- linq select items where property of items in list not in properties of items in another list
- Which of the two LINQ Statements is would work faster?
- No property or field exists in type dynamic class
- Returning value from array
- Changing data structure using convertAll
- LINQ and xml data
- Issues with ListView here in C#
- LINQ .Count() not defined for int
- LINQ xml finding nodes returns null
- LINQ query return
- choose a few id from db C#
- Dynamic Order By Entity Framework
- An Optimized algorithm for finding all Sundays and Fridays for a given interval
- Select From DataTable with Multiple Conditions