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 to xml: result based on attributes
- Insert to multiple tables from the same view using LINQ to SQL
- How could I create single list from Parent and child node from XML using Linq
- how to use Linq to get a sitecore field
- Test sql connect without using System.Data.SqlClient
- how to change the value of dictionary using linq based on key?
- How to use Ternary operation for Linq insertion from form Correctly
- How to break link between source collection and LINQ result
- Generating very large XML file with Linq-to-XML and Linq-to-SQL
- Does Castle ActiveRecord's ActiveRecordMediator<> support LINQ?
- LINQ: Get the path in a hierarchical structure collection item
- Way to convert this to LINQ conditionals
- Better Linq parsing of Large XML data
- C# Linq SQL shows the query as page content
- Left (Outer) Joins with Linq - Contradiction with "into" in examples I found
- LINQ Lambda where in subquery
- Entity Framework: What is use /Meaning of (?) question mark
- Does Linq Remove The Need For Hibernate?
- Read two lines before last line of text file
- Convert lambda to expression tree
- The union of the intersects of the 2 set combinations of a sequence of sequences
- Query from db with entity - one to one relationship
- linq lambda composite key where and group by
- LINQ2SQL Any(), NOT EXISTS Problems
- How to sort files by date in file name using c#?
- error: The query results cannot be enumerated more than once
- C# linq group by on different keys in the same entity
- LINQ Group By on DateTime
- Use of Take() LINQ Method in Entity Framework Core v3.1.4
- How to create a List<int> basead on two properties from another list?