linq-to-objects

Linq select objects in list where exists IN (A,B,C)

[亡魂溺海] 提交于 2019-11-26 21:42:44
I have a list of orders . I want to select orders based on a set of order statuses. So essentially select orders where order.StatusCode in ("A", "B", "C") // Filter the orders based on the order status var filteredOrders = from order in orders.Order where order.StatusCode.????????("A", "B", "C") select order; Your status-codes are also a collection, so use Contains : var allowedStatus = new[]{ "A", "B", "C" }; var filteredOrders = orders.Order.Where(o => allowedStatus.Contains(o.StatusCode)); or in query syntax: var filteredOrders = from order in orders.Order where allowedStatus.Contains(order

How can I get LINQ to return the object which has the max value for a given property? [duplicate]

断了今生、忘了曾经 提交于 2019-11-26 21:37:09
This question already has an answer here: How to perform .Max() on a property of all objects in a collection and return the object with maximum value [duplicate] 9 answers How to use LINQ to select object with minimum or maximum property value 12 answers If I have a class that looks like: public class Item { public int ClientID { get; set; } public int ID { get; set; } } And a collection of those items... List<Item> items = getItems(); How can I use LINQ to return the single "Item" object which has the highest ID? If I do something like: items.Select(i => i.ID).Max(); I'll only get the highest

Select distinct by two properties in a list

扶醉桌前 提交于 2019-11-26 20:29:56
I have a list<message> that contains properties of type Guid and DateTime (as well as other properties). I would like to get rid of all of the items in that list where the Guid and DateTime are the same (except one). There will be times when those two properties will be the same as other items in the list, but the other properties will be different, so I can't just use .Distinct() List<Message> messages = GetList(); //The list now contains many objects, it is ordered by the DateTime property messages = from p in messages.Distinct( what goes here? ); This is what I have right now, but it seems

C# Ranking of objects, multiple criteria

爱⌒轻易说出口 提交于 2019-11-26 20:15:57
问题 I am building a plugin for a LAN party website that I wrote that would allow the use of a Round Robin tournament. All is going well, but I have some questions about the most efficient way to rank over two criteria. Basically, I would like the following ranking layout: Rank Wins TotalScore PersonE 1 5 50 PersonD 2 3.5 37 PersonA 2 3.5 37 PersonC 4 2.5 26 PersonB 5 2.5 24 PersonF 6 0 12 In SQL server, I would use: SELECT [Person], RANK() OVER (ORDER BY Wins DESC, TotalScore DESC) [Rank], [Wins]

How to create a dynamic LINQ join extension method

给你一囗甜甜゛ 提交于 2019-11-26 19:46:57
There was a library of dynamic LINQ extensions methods released as a sample with Visual Studio 2008 . I'd like to extend it with a join method. The code below fails with a parameter miss match exception at run time. Where is the problem? public static IQueryable Join(this IQueryable outer, IEnumerable inner, string outerSelector, string innerSelector, string resultsSelector, params object[] values) { if (inner == null) throw new ArgumentNullException("inner"); if (outerSelector == null) throw new ArgumentNullException("outerSelector"); if (innerSelector == null) throw new ArgumentNullException

Linq OrderBy against specific values

给你一囗甜甜゛ 提交于 2019-11-26 19:23:20
问题 Is there a way in Linq to do an OrderBy against a set of values (strings in this case) without knowing the order of the values? Consider this data: A B A C B C D E And these variables: string firstPref, secondPref, thirdPref; When the values are set like so: firstPref = 'A'; secondPref = 'B'; thirdPref = 'C'; Is it possible to order the data like so: A A B B C C D E 回答1: If you put your preferences into a list, it might become easier. List<String> data = new List<String> { "A","B","A","C","B"

Code equivalent to the 'let' keyword in chained LINQ extension method calls

两盒软妹~` 提交于 2019-11-26 18:47:04
问题 Using the C# compilers query comprehension features, you can write code like: var names = new string[] { "Dog", "Cat", "Giraffe", "Monkey", "Tortoise" }; var result = from animalName in names let nameLength = animalName.Length where nameLength > 3 orderby nameLength select animalName; In the query expression above, the let keyword allows a value to be passed forward to the where and orderby operations without duplicate calls to animalName.Length . What is the equivalent set of LINQ extension

LINQ to SQL and a running total on ordered results

时光总嘲笑我的痴心妄想 提交于 2019-11-26 17:30:06
I want to display a customer's accounting history in a DataGridView and I want to have a column that displays the running total for their balance. The old way I did this was by getting the data, looping through the data, and adding rows to the DataGridView one-by-one and calculating the running total at that time. Lame. I would much rather use LINQ to SQL, or LINQ if not possible with LINQ to SQL, to figure out the running totals so I can just set DataGridView.DataSource to my data. This is a super-simplified example of what I'm shooting for. Say I have the following class. class Item { public

LINQ identity function?

淺唱寂寞╮ 提交于 2019-11-26 16:57:42
问题 Just a little niggle about LINQ syntax. I'm flattening an IEnumerable<IEnumerable<T>> with SelectMany(x => x) . My problem is with the lambda expression x => x . It looks a bit ugly. Is there some static 'identity function' object that I can use instead of x => x ? Something like SelectMany(IdentityFunction) ? 回答1: Note: this answer was correct for C# 3, but at some point (C# 4? C# 5?) type inference improved so that the IdentityFunction method shown below can be used easily. No, there isn't.

Lambda Expression for join

谁都会走 提交于 2019-11-26 16:07:53
问题 public class CourseDetail { public CourseDetail(); public string CourseId { get; set; } public string CourseDescription { get; set; } public long CourseSer { get; set; } } public class RefUIDByCourse { public long CourseSer { get; set; } public double DeliveredDose{ get; set; } public double PlannedDose{ get; set; } public string RefUID { get; set; } } public class RefData { public double DailyDoseLimit { get; set; } public string RefName { get; set; } public string RefUID { get; set; }