linq

How do I get the Nth largest element from a list with duplicates, using LINQ?

烈酒焚心 提交于 2021-02-09 10:58:31
问题 I've seen in various StackOverflow answers that I can get the Nth largest element in a list by doing something like var nthFromTop = items.OrderByDescending().Skip(N-1).First(); But wouldn't this only work if there are no duplicates in the list? If the list contains duplicates, is there a way to get the Nth largest element (or set of elements) using LINQ? If not, what would be the most efficient way to do this in C#? 回答1: To get the set of all items equal to the Nth largest item you'll need

Lambda创建表达式目录树

风流意气都作罢 提交于 2021-02-09 03:39:13
一,如下代码: using System; using System.Linq.Expressions; namespace Demo { class Program { static void Main( string [] args) { Func < int , int , int > func = null ; // 两个参数 ParameterExpression a = Expression.Parameter( typeof ( int ), " a " ); ParameterExpression b = Expression.Parameter( typeof ( int ), " b " ); // 求积 BinaryExpression cj = Expression.Multiply(a, b); // 常量 ConstantExpression Con2 = Expression.Constant( 4 ); // 求和 BinaryExpression Add = Expression.Add(cj, Con2); // 创建一个表示lambda表达式的对象 Expression<Func< int , int , int >> lexp = Expression.Lambda<Func< int , int , int >> (cj, a, b);

LINQ Query to put data in a nested object

 ̄綄美尐妖づ 提交于 2021-02-09 00:34:23
问题 I am working on writing a C# LINQ query to get data from a SQL table below Table1 Id Status SubStatus 1 Review Pending 2 Review Complete 3 Review Approved 4 Review Denied 5 Info Processing 6 Info Pending 7 Info Requested 8 Info Received 9 Approval Approved 10 Review Approved From the above table I would like to put the data in an object like below public class LastStatusDto { public string StatusName { get; set; } public int StatusId { get; set; } public int Count { get; set; } public

LINQ Query to put data in a nested object

左心房为你撑大大i 提交于 2021-02-09 00:30:39
问题 I am working on writing a C# LINQ query to get data from a SQL table below Table1 Id Status SubStatus 1 Review Pending 2 Review Complete 3 Review Approved 4 Review Denied 5 Info Processing 6 Info Pending 7 Info Requested 8 Info Received 9 Approval Approved 10 Review Approved From the above table I would like to put the data in an object like below public class LastStatusDto { public string StatusName { get; set; } public int StatusId { get; set; } public int Count { get; set; } public

LINQ Query to put data in a nested object

一个人想着一个人 提交于 2021-02-09 00:21:52
问题 I am working on writing a C# LINQ query to get data from a SQL table below Table1 Id Status SubStatus 1 Review Pending 2 Review Complete 3 Review Approved 4 Review Denied 5 Info Processing 6 Info Pending 7 Info Requested 8 Info Received 9 Approval Approved 10 Review Approved From the above table I would like to put the data in an object like below public class LastStatusDto { public string StatusName { get; set; } public int StatusId { get; set; } public int Count { get; set; } public

Implicit Cast not happening in Expression Tree

老子叫甜甜 提交于 2021-02-08 13:56:49
问题 I came across a scenario where I need to sort a list of custom type on different properties based on input. With the help of few articles, I was able to come up with generic implementation using LINQ.During unit testing, one of the test failed because implicit conversion was happening when lamda expression was created using Expression tree. Below I have put the sample code to understand the issue (Not sure why formatting was not getting correct, sorry for that) static class ExtensionMethods {

How to get only specific field from the list

☆樱花仙子☆ 提交于 2021-02-08 13:34:06
问题 I have an IEnumerable of Lesson objects: IEnumerable<Lesson> filteredLessons I convert it to a List through the following method: ToList(); But I want the returned list to contain only the first property, lessonid , not all the Lesson properties. How can I get the data of specific property of the list instead of the objects? 回答1: You can select the value you want first, like this: filteredLessons.Select(l => l.lessonId).ToList(); And you'll get a list of ID's 回答2: If you want to get the the

LINQ version of calculating sum product of adjacent elements in an array

时光总嘲笑我的痴心妄想 提交于 2021-02-08 13:28:34
问题 I am looking for a way to do the following in LINQ (it is basically a sumproduct where the same array contains both operands of the product at n and n + 1 indices, obviously the array length is always 2 * n ). int[] input = { -3, 26, -2, 19, 2, 19, 3, 29, 1, 48 }; // sum product double result = 0; for (int i = 0; i < input.Length; i += 2) result += input[i] * input[i + 1]; // result = 57 // linq version of calculating resultPrice ??? How can I do this (elegantly) with LINQ? 回答1: You can Zip

Summing a value inside of a Anonymous Type

杀马特。学长 韩版系。学妹 提交于 2021-02-08 12:10:06
问题 I have this in my code: var pRating = ctx.PrProjectRating .Where(x => x.PrIsDeleted == false) .Select(k => k) .GroupBy(g => new {g.PrPIdG}, (key, group) => new { sumR = group.Sum(k => k.PrValue), pidG = key.PrPIdG }); var pLike = ctx.PlProjectLike .Where(x => x.PlValue == "Like") .Select(c => c) .GroupBy(g => new {g.PlPIdG}, (key, group) => new { sumR = group.Count(), pidG = key.PlPIdG }) .OrderByDescending(g => g.sumR); var pConnect = ctx.PcProjectConnect .Where(x => x.PcStatus == "Connected

Summing a value inside of a Anonymous Type

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-08 12:05:38
问题 I have this in my code: var pRating = ctx.PrProjectRating .Where(x => x.PrIsDeleted == false) .Select(k => k) .GroupBy(g => new {g.PrPIdG}, (key, group) => new { sumR = group.Sum(k => k.PrValue), pidG = key.PrPIdG }); var pLike = ctx.PlProjectLike .Where(x => x.PlValue == "Like") .Select(c => c) .GroupBy(g => new {g.PlPIdG}, (key, group) => new { sumR = group.Count(), pidG = key.PlPIdG }) .OrderByDescending(g => g.sumR); var pConnect = ctx.PcProjectConnect .Where(x => x.PcStatus == "Connected