score:1
here is my take :
var numlist = new list<int> { 12, 33, 24, 63, 45, 32, 3, 18, 22, 7, 10 };
var answers = (from a in numlist
from b in numlist where a >= b
from c in numlist where b >= c
from d in numlist where c >= d
select new { sum = a + b + c + d, ans = "{" + a + " " + b + " " + c + " " + d + "}"} into temp
where temp.sum > 100
select temp.ans).distinct() ;
foreach (var answer in answers)
console.writeline(answer);
output is truncated (148 total):
{33 32 24 12}
{33 32 24 18}
{33 32 24 22}
{33 32 22 18}
{63 33 12 3}
{63 33 12 7}
{63 33 12 10}
{63 33 24 12}
{63 33 24 3}
{63 33 24 18}
{63 33 24 22}
{63 33 24 7}
{63 33 24 10}
{63 33 32 12}
{63 33 32 24}
{63 33 32 3}
{63 33 32 18}
{63 33 32 22}
{63 33 32 7}
{63 33 32 10}
{63 33 18 12}
{63 33 18 3}
{63 33 18 7}
{63 33 18 10}
{63 33 22 12}
{63 33 22 3}
{63 33 22 18}
{63 33 22 7}
{63 33 22 10}
{63 33 7 3}
{63 33 10 3}
{63 33 10 7}
{63 24 12 3}
{63 24 12 7}
{63 24 12 10}
{63 24 18 12}
{63 24 18 3}
score:3
probably this is not an effient way but it should work:
var numlist = new list<int>() { 12, 33, 24, 63, 45, 32, 3, 18, 22, 7, 10 };
/* get all combinations including duplicates like:
12,12,12,12 - 12,12,12,34 - 12,12,12,24 and so on
then put them into an array int[] */
var combinations = from x in numlist
from y in numlist
from z in numlist
from t in numlist
select new [] {x, y, z, t};
/* eliminate the duplicates (like 12-12-12-12) and
filter them based on sum */
var result = combinations
.where(x => x.sum() > 100 && x.distinct().count() == x.length);
// get distinct combinations using a custom equality comparer
var distinctresults = result.distinct(new comparer()).tolist();
public class comparer : iequalitycomparer<int[]>
{
public bool equals(int[] x, int[] y)
{
return x.orderby(a => a).sequenceequal(y.orderby(a => a));
}
public int gethashcode(int[] obj)
{
return obj.select(x => x.gethashcode()).sum();
}
}
this use of from
clause is called compound from clause
which allows you to get combinations easily, you can refer to msdn documentation to see more examples also this article from eric lippert might be helpful.
Source: stackoverflow.com
Related Query
- LINQ Expression to extract specific quantity and value of numbers
- Get Value and Count of that value using LINQ or lambda expression
- Is possible with LINQ to extract from an array of objects the value of one property and create a new array?
- How to extract a specific value from the linq query?
- C# LINQ Arrange Multiple Columns Value and Resign to Specific Columns
- loop through specific attribute using xml linq and c# and get its lowest value
- LINQ Expression to get member value and can be translated to SQL?
- How to extract specific value from this json string using linq
- Using LINQ Lambda expression determining value by group by and where condition
- Linq group by specific property of a class and then take min and max value
- Cannot implilcitly convert type int? to int when retrieving and int field value from LINQ expression
- Convert a filter expression based on property name and value to Linq Where clause Func<T, bool>?
- c# Linq or code to extract groups from a single list of source data
- Join Two Array based on Index and Index Value using Linq Query Expression Syntax
- Build Dynamic Expression For Linq Where using PropertyInfo and a value
- Average numbers associated with same date and extract date from datetime using linq
- Extract key and value from a List of a List of objects using LINQ
- How to get the value of elements using XML and Linq based off a specific tag name
- How to parse a xml-file using C# and Linq by finding a specific attributes value
- LINQ expression to check against a collection and set a default value when the check fails
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- Like in Lambda Expression and LINQ
- What is the Efficiency and Performance of LINQ and Lambda Expression in .Net?
- Populate a list with a specific range of numbers by using LINQ
- The LINQ expression could not be translated and will be evaluated locally
- Selecting multiple columns with linq query and lambda expression
- Find an XElement with a certain attribute name and value with LINQ
- Get the parameter value from a Linq Expression
- LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression
More Query from same tag
- Top 5 LINQ query with group by using Entity Framework
- How to get a list of the grouped values in linq groupby?
- Implementation for a method that finds the first nonrepeatable character in a string using Linq, while crossing through the string only once
- How to search through children JSON with LINQ
- Get a specific info from an other table with Linq
- How do I nest this LINQ query?
- How can I use order by and group by in Linq Extraction Method and With multiple data?
- Group by and select in LINQ
- How can I solve 'Invalidcastexception'
- Dynamic linq-to-nhibernate query problem
- LINQ query not showing results - LEFT join with WHERE clause
- Databind gridview with LINQ
- How do LINQ queries against the Entity Framework communicate dates to a SQL Server?
- How to return Generic.List<Anonymoustype> from a function in C#
- EXECUTE sp_executesql @SQL Linq returns VOID
- Pimp my LINQ: a learning exercise based upon another post
- How do I sort a list and its children and their children using linq?
- Linq to sum on groups based on two columns
- Flatten a Dictionary<int, List<object>>
- Subtracting days from a date using linq
- Returns “Enumeration yielded no results” in LINQ statement
- Union two nested List<object> with LINQ
- running total by linq : place results in paged List
- Shortcut LINQ toentity query for the following code in MVC 3
- Using LINQ to find a common prefix?
- Check if two dates completely overlaps a date range
- Compare in Linq
- LinqDataSource - Can you limit the amount of records returned?
- Select in Method Syntax C# - returns a collection of anonymous object
- Changing a linq query to filter on many-many