Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 72023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:58:46+00:00 2026-05-10T19:58:46+00:00

I have two lists of custom objects and want to update a field for

  • 0

I have two lists of custom objects and want to update a field for all objects in one list if there is an object in the other list which matches on another pair of fields.

This code explains the problem better and produces the results I want. However for larger lists 20k, and a 20k list with matching objects, this takes a considerable time (31s). I can improve this with ~50% by using the generic lists Find(Predicate) method.

using System; using System.Linq; using System.Linq.Expressions; using System.Collections.Generic; namespace ExperimentFW3 {     public class PropValue     {         public string Name;         public decimal Val;         public decimal Total;     }     public class Adjustment     {         public string PropName;         public decimal AdjVal;     }     class Program     {         static List<PropValue> propList;         static List<Adjustment> adjList;          public static void Main()         {             propList = new List<PropValue>{                 new PropValue{Name = 'Alfa', Val=2.1M},                 new PropValue{Name = 'Beta', Val=1.0M},                 new PropValue{Name = 'Gamma', Val=8.0M}             };             adjList = new List<Adjustment>{                 new Adjustment{PropName = 'Alfa', AdjVal=-0.1M},                 new Adjustment{PropName = 'Beta', AdjVal=3M}             };              foreach (var p in propList)             {                 Adjustment a = adjList.SingleOrDefault(                     av => av.PropName.Equals(p.Name)                     );                 if (a != null)                     p.Total = p.Val + a.AdjVal;                 else                     p.Total = p.Val;             }         }     } } 

The desired result is: Alfa total=2,Beta total=4,Gamma total=8

But I wonder if this is possible to do even faster. Inner joining the two lists takes very little time, even when looping over 20k items in the resultset.

var joined = from p in propList              join a in adjList on p.Name equals a.PropName              select new { p.Name, p.Val, p.Total, a.AdjVal }; 

So my question is if it’s possible to do something like I would do with T-SQL? An UPDATE from a left join using ISNULL(val,0) on the adjustment value.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:58:47+00:00Added an answer on May 10, 2026 at 7:58 pm

    That join should be fairly fast, as it will first loop through all of adjList to create a lookup, then for each element in propList it will just use the lookup. This is faster than your O(N * M) method in the larger code – although that could easily be fixed by calling ToLookup (or ToDictionary as you only need one value) on adjList before the loop.

    EDIT: Here’s the modified code using ToDictionary. Untested, mind you…

    var adjDictionary = adjList.ToDictionary(av => av.PropName); foreach (var p in propList) {     Adjustment a;     if (adjDictionary.TryGetValue(p.Name, out a))     {         p.Total = p.Val + a.AdjVal;     }     else     {         p.Total = p.Val;     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one custom action (add) and two custom objects (favorites list) and (tv
I'm trying to create a simple, custom, gallery. I have two lists, one with
I have two lists. They are sortable but not connected (elements of list one
I have two lists which are guaranteed to be the same length. I want
So I have a custom ListView object. The list items have two textviews stacked
I have a list of custom objects: say Fruits with two string properties Name
I have a strongly typed list of custom objects, MyObject , which has a
I have custom Question objects which I render into html form elements. I want
I have a list of custom objects with two properties as identifiers (IDa and
I have two Lists, they look like this <List> ads [0] Headline = Sony

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.