linq-to-objects

Aggregate values until a limit is reached

痴心易碎 提交于 2019-12-05 17:47:13
I need something similar to an AggregateWhile method. The standard System.Linq.Enumerable class doesn't provide it. Until now I've always been able to leverage the standard LINQ methods to solve every problem I've encountered. So I'd like to know if that's still possible in this case, or if I really do need to extend LINQ with a non-standard method. The hypothetical AggregateWhile method would iterate over a sequence and apply the accumulator. The aggregation would be complete once a predicate returns false. The result is the aggregration of elements up to but not including the element for

How can I get LINQ to return the index of the object which has the max value in a collection?

为君一笑 提交于 2019-12-05 14:15:28
I have a list of immutable objects (in my specific case a list of Tuple<double, double> ) and I'd like to change the one with the highest Item2 value. Ideally there would be an IndexOfMaxBy function I could use, so I could do: var indexOfPointWithHighestItem2 = myList.IndexOfMaxBy(x => x.Item2); var original = myList[indexOfPointWithHighestItem2]; myList[indexOfPointWithHighestItem2] = new Tuple<double, double>(original.Item1, original.Item2 - 1); I have seen How can I get LINQ to return the object which has the max value for a given property? , and using Jon Skeet 's MaxBy function combined

Return min value in group with lambda/linq query

孤街浪徒 提交于 2019-12-05 06:48:26
I need help creating a lambda expression to query the following list for retrieving the lowest priced item in each channel. Ie for this example item A, D and G class Radio { public string Name { get; set; } public int Channel { get; set; } public decimal Price { get; set; } } List<Radio> radios = new List<Radio>(); radios.Add(new Radio() { Name = "A", Channel = 1, Price = 10 }); radios.Add(new Radio() { Name = "B", Channel = 1, Price = 20 }); radios.Add(new Radio() { Name = "C", Channel = 1, Price = 30 }); radios.Add(new Radio() { Name = "D", Channel = 2, Price = 10 }); radios.Add(new Radio()

LINQ query with multiple aggregates

本小妞迷上赌 提交于 2019-12-05 06:29:35
How would I create the equivalent Linq To Objects query? SELECT MIN(CASE WHEN p.type = "In" THEN p.PunchTime ELSE NULL END ) AS EarliestIn, MAX(CASE WHEN p.type = "Out" THEN p.PunchTime ELSE NULL END ) AS LatestOUt FROM Punches p You can't efficiently select multiple aggregates in vanilla LINQ to Objects. You can perform multiple queries, of course, but that may well be inefficient depending on your data source. I have a framework which copes with this which I call "Push LINQ" - it's only a hobby (for me and Marc Gravell) but we believe it works pretty well. It's available as part of MiscUtil

Using LINQ to objects Intersect and Except on a specific property

风格不统一 提交于 2019-12-05 06:12:22
When I have 2 List<string> objects, then I can use Intersect and Except on them directly to get an output IEnumerable<string> . That's simple enough, but what if I want the intersection/disjuction on something more complex? Example, trying to get a collection of ClassA objects which is the result of the intersect on ClassA object's AStr1 and ClassB object's BStr ; : public class ClassA { public string AStr1 { get; set; } public string AStr2 { get; set; } public int AInt { get; set; } } public class ClassB { public string BStr { get; set; } public int BInt { get; set; } } public class Whatever

Does Enumerable.Where in LINQ-to-objects preserve order? [duplicate]

心已入冬 提交于 2019-12-05 05:47:11
This question already has an answer here: Preserving order with LINQ 6 answers var source = new List<string> { "A1", "A2", "B1", "B2" }; var filtered = source.Where(s => s.StartsWith("A")); foreach (var s in filtered) Console.WriteLine(s); // outputs first A1 and then A2 It seems like Enumerable.Where keeps the original order of elements when used on an ordered IEnumerable (such as a List<T> or T[] ). Is this always the case? If yes, where is this documented? Stephan Seefeld Microsoft does actually document that LINQ to Objects preserves ordering. The document http://msdn.microsoft.com/en-us

LINQ-to-objects index within a group + for different groupings (aka ROW_NUMBER with PARTITION BY equivalent)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 05:08:23
After much Google searching and code experimentation, I'm stumped on a complex C# LINQ-to-objects problem which in SQL would be easy to solve with a pair of ROW_NUMBER()...PARTITION BY functions and a subquery or two. Here's, in words, what I'm trying to do in code-- the underlying requirement is removing duplicate documents from a list: First, group a list by (Document.Title, Document.SourceId), assuming a (simplified) class definition like this: class Document { string Title; int SourceId; // sources are prioritized (ID=1 better than ID=2) } Within that group, assign each document an index

Count values in Dictionary using LINQ and LINQ extensions

心不动则不痛 提交于 2019-12-05 02:26:38
I have a dictionary that looks something like this: Dictionary<String, List<String>> test1 : 1,3,4,5 test2 : 2,3,6,7 test3 : 2,8 How can I get a count of all the values using LINQ and LINQ extensions? Ani Let's say you have: Dictionary<String, List<String>> dict = ... If you want the number of lists, it's as simple as: int result = dict.Count; If you want the total count of all the strings in all the lists: int result = dict.Values.Sum(list => list.Count); If you want the count of all the distinct strings in all the lists: int result = dict.Values .SelectMany(list => list) .Distinct() .Count()

Error in LINQ Left JOIN

痴心易碎 提交于 2019-12-05 00:51:04
i have written below query in LINQ to perform left join but its throwing error: var qry = from c in dc.category_feature_Name_trans_SelectAll_Active() join p in dc.product_category_feature_trans_SelectAll() on c.cft_id equals p.cft_id into cp from p in cp.DefaultIfEmpty() select new { c.cft_id, c.feature_id, c.feature_name, p.product_id , p.value }; Error: Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in

get next available integer using LINQ

久未见 提交于 2019-12-05 00:10:05
问题 Say I have a list of integers: List<int> myInts = new List<int>() {1,2,3,5,8,13,21}; I would like to get the next available integer, ordered by increasing integer. Not the last or highest one, but in this case the next integer that is not in this list. In this case the number is 4. Is there a LINQ statement that would give me this? As in: var nextAvailable = myInts.SomeCoolLinqMethod(); Edit: Crap. I said the answer should be 2 but I meant 4. I apologize for that! For example: Imagine that