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 6708263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:44:16+00:00 2026-05-26T07:44:16+00:00

I have a List<String> and two List<CustomObject> . I want to combine two List<CustomObject>

  • 0

I have a List<String> and two List<CustomObject>.
I want to combine two List<CustomObject> but only if second List<CustomObject> property is not contained in List<String>.

So, want to do something like this:

resultSet1.AddRange(resultSet2.Select(x => !names.Contains(x.City)).ToList());

What am I missing here?

  • 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-05-26T07:44:17+00:00Added an answer on May 26, 2026 at 7:44 am

    You are misusing Select when you should be using Where.

    resultSet1.AddRange(resultSet2.Where(x => !names.Contains(x.City));
    

    Select is a projection method. It takes a sequence and selects it, sometimes selecting the entire item, a single property, or projecting it into something else entirely.

    Where is a filtering method. It takes a sequence and applies a predicate to it, yielding only the elements that pass the predicate.

    (In your example, by using Select, you’re effectively trying to add a list of bool to a list of CustomObject, which is not going to work.)


    Not addressing the specific error at hand, here are some additional ideas to consider.

    This is a situation where a HashSet<string> might be beneficial for names, particularly if names is significantly large. Contains on a HashSet<T> is of O(1) complexity, whereas it is O(n) for List<T>. However, there is overhead associated with the HashSet, so if you have any concerns, it’s best to measure both and see which is more performant.

    One more thing that might help, if you simply need to stream one sequence after the other and do not necessarily need to change or add to either collection, you might consider using Union or Concat operations.

    var sequence = resultSet1.Union(resultSet2.Where(x => !names.Contains(x.City)));
    var sequence = resultSet1.Concat(resultSet2.Where(x => !names.Contains(x.City)));
    

    The difference in the two being that Union will filter out any duplicates in the resulting sequence (from both inputs, not just the second against the first), Concat applies no duplicate-filtering logic and simply streams one sequence after the other. The input sequences (resultSet1 and resultSet2) are unmodified.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list like this Dim emailList as new List(Of String) emailList.Add(one@domain.com) emailList.Add(two@domain.com)
I have two List of array string. I want to be able to create
I have a Dictionary of dictionaries : SortedDictionary<int,SortedDictionary<string,List<string>>> I would like to merge two
Question is simple: I have two List List<String> columnsOld = DBUtils.GetColumns(db, TableName); List<String> columnsNew
I have two complex (i.e. objects with string, int, double, List and other home
I have a list of string values that I want add to a hashtable
I have a String List with items like this Root Root/Item1 Root/Item2 Root/Item3/SubItem1 Root/Item3/SubItem2
I have a list of string tuples, say (P1,P2) I'd like to know if
Can I have a List containing one string and two numbers? Or I can
I have two lists List<WorkOrder> and List<PlannedWork> I would like join the two lists

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.