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

  • SEARCH
  • Home
  • 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 8855459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:08:23+00:00 2026-06-14T14:08:23+00:00

I have 2 lists that I need to consolidate. List 1 has only the

  • 0

I have 2 lists that I need to consolidate. List 1 has only the dates, and List 2 may have the time element as well:

var List1 = new[] { 
  new ListType{ val = new DateTime(2012, 1, 1)}, 
  new ListType{ val = new DateTime(2012, 1, 2)} 
};

List2 = new[] { new ListType{ val = new DateTime(2012, 1, 1, 5, 0, 0)} };

FinalList = new[] { 
  new ListType{ val = new DateTime(2012, 1, 1, 5, 0, 0)}, 
  new ListType{ val = new DateTime(2012, 1, 2)} 
};

The way I’m going about this is:

foreach (var l in List1) {
  var match = List2.FirstOrDefault(q => q.val.Date == l.val);
  if (match == null) continue;
  l.val = match.val;
}

Is there a better way than iterating through List1, using FirstOrDefault and then reassigning the val? It works, so this is just more a curiosity if Linq has a more elegant way (i.e. I am missing something obvious).

Thanks

  • 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. Editorial Team
    Editorial Team
    2026-06-14T14:08:24+00:00Added an answer on June 14, 2026 at 2:08 pm

    You can use Enumerable.Union with a custom IEqualityComparer<ListType>:

    class ListType
    {
        public DateTime val { get; set; }
    
        public class DateComparer : IEqualityComparer<ListType>
        {
            public bool Equals(ListType x, ListType y)
            {
                if (ReferenceEquals(x, y))
                    return true;
                else if (x == null || y == null)
                    return false;
                return x.val.Date == y.val.Date;
            }
    
            public int GetHashCode(ListType obj)
            {
                return obj.val.Date.GetHashCode();
            }
        }
    }
    

    and then …

    var finalList = List2.Union(List1, new ListType.DateComparer());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two lists that i need to combine where the second list has
So I have a subclassed UITableView that lists data. I need to make only
I have a list of images that need to be displayed. However theres only
I have a drop down list that has options that need to be passed
I have a list of pointers that reference time objects that need a turn
I have 8 sorted lists that I need to merge into 1 sorted list.
I have 2 lists: first is list that I need to work with and
I have two lists List that I need to combine in third list and
I have 2 lists that have dates and data. Each list is in the
I have 2 lists of different objects, that I need to join on 2

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.