linq-to-objects

Use LINQ and C# to make a new List from an old List

坚强是说给别人听的谎言 提交于 2019-11-27 18:21:48
问题 This should be pretty simple, but I am new at LINQ. I have a List<FillStruct> of FillList structs. I'd like to use LINQ to create a new List<NewFillStruct> where instead of having the number of buys and sells, I would have one variable containing the sum. For example, if the FillStruct structure has buy = 4 and sell = 2 then the NewFillStruct structure will have numlong = 2. If the FillStruct structure has buy = 2 and sell = 4 then the NewFillStruct structure will have numlong = -2. Here are

Distinct in LINQ with anonymous types (in VB.NET)

假如想象 提交于 2019-11-27 18:16:04
问题 Supposing the referenced List below contains 2 elements: Dim Countries = From c In List _ Select New With { .Country = c.Country, .CountryID = c.CountryID } the code above returns .Country=Spain .CountryID = 1 .Country=Spain .CountryID = 1 How can i get the distinct values? The Countries query should contain only .Country=Spain .CountryID = 1 回答1: I can only assume you're dead set on the use of anonymous type as the answer given by Alex Peck is correct. (and I've upvoted it). However, this

How to use LINQ to select all descendants of a composite object

南笙酒味 提交于 2019-11-27 17:02:34
问题 How can I make ComponentTraversal.GetDescendants() better using LINQ? Question public static class ComponentTraversal { public static IEnumerable<Component> GetDescendants(this Composite composite) { //How can I do this better using LINQ? IList<Component> descendants = new Component[]{}; foreach(var child in composite.Children) { descendants.Add(child); if(child is Composite) { descendants.AddRange((child as Composite).GetDescendants()); } } return descendants; } } public class Component {

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

大兔子大兔子 提交于 2019-11-27 16:54:01
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 method calls that achieves what the "let" keyword does here? Let doesn't have its own operation; it

What's your favorite LINQ to Objects operator which is not built-in?

非 Y 不嫁゛ 提交于 2019-11-27 16:39:02
With extension methods, we can write handy LINQ operators which solve generic problems. I want to hear which methods or overloads you are missing in the System.Linq namespace and how you implemented them. Clean and elegant implementations, maybe using existing methods, are preferred. Timwi Append & Prepend /// <summary>Adds a single element to the end of an IEnumerable.</summary> /// <typeparam name="T">Type of enumerable to return.</typeparam> /// <returns>IEnumerable containing all the input elements, followed by the /// specified additional element.</returns> public static IEnumerable<T>

Linq Group on Multiple Fields - VB.NET, Anonymous, Key

♀尐吖头ヾ 提交于 2019-11-27 15:57:03
问题 I am stumped. I need help. I have a DTO object with duplicates patient address data. I need to get only the unique addresses. Dim PatientAddressDto = New List(Of PatientAddress) {Populate PatientAddressDto with lots of duplicate data} PatientAddressDto = (From d In PatientAddressDto Group d By PatientAddressDtoGrouped = New PatientAddress With { .Address1 = d.Address1, .Address2 = d.Address2, .City = d.City, .State = d.State, .Zip = d.Zip } Into Group Select New PatientAddress With {

LINQ identity function?

泄露秘密 提交于 2019-11-27 14:56:11
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) ? 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. It would have to be generic, to start with: public static Func<T, T> IdentityFunction<T>() { return x => x;

Linq to objects Predicate Builder

允我心安 提交于 2019-11-27 13:21:46
What is the best way to do a conditional query using linq to objects(not linq to sql). Currently I am using the Predicate builder found here http://www.albahari.com/nutshell/predicatebuilder.aspx and passing the compiled predicate to the IEnumerable.Where and it seems to work nicely. Example code of what I want to solve: eg I have this string keyword1 = "Test1"; string keyword2 = "Test3"; IEnumerable<TestObject> tests = new List<TestObject>() { new TestObject() {Name1 = "Test1", Name2 = "Test1"}, new TestObject() {Name1 = "Test2", Name2 = "Test2"}, new TestObject() {Name1 = "Test3", Name2 =

Check if all items are the same in a List

一世执手 提交于 2019-11-27 12:59:38
I have a List(Of DateTime) items. How can I check if all the items are the same with a LINQ query? At any given time there could be 1, 2, 20, 50 or 100 items in the list. Thanks Like this: if (list.Distinct().Skip(1).Any()) Or if (list.Any(o => o != list[0])) (which is probably faster) I created simple extension method mainly for readability that works on any IEnumerable. if (items.AreAllSame()) ... And the method implementation: /// <summary> /// Checks whether all items in the enumerable are same (Uses <see cref="object.Equals(object)" /> to check for equality) /// </summary> /// <typeparam

Lambda Expression for join

前提是你 提交于 2019-11-27 12:47:10
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; } public double SessionDoseLimit { get; set; } } public class CourseSummary { public long CourseSer { get; set