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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:46:16+00:00 2026-06-15T04:46:16+00:00

I am going to use some basic stripped down examples to illustrate my problem.

  • 0

I am going to use some basic stripped down examples to illustrate my problem.

I have a class:

class Item
{
   int ID;
   bool Selected;
}

Now lets say I have two lists of the Item class:

List<Item> ListA = GetListA();
List<Item> ListB = GetListB();

Now I want to create a third list that has all the items from ListB. The important thing is that if a match (same ID) if found in ListA then I want to use that Selected value, otherwise I want to keep the Selected value of the item that is in ListB.

I am creating the third list as follows:

List<Item> ListC = from item in ListB 
select new Item
{
   ID = item.ID,
   Selected = item.Selected// <-- should use value form ListA if available
};

Important: I don’t want to seem ignorant, but I do not want to change the way ListC is created. By that I mean I want to use the “linq select” method, and I want to use a “one liner” that assigns the Selected value… I know there are other ways to create the list which will work just fine, but then I won’t learn anything new.


I have tried a couple of things so far…

I know this will work, but I don’t want to query ListA twice:

Selected = ListA.Any(x => x.ID == item.ID) ? ListA.First(x => x.ID == item.ID).Selected : item.Selected

and I also tried using DeafultIfEmpty but I don’t think that is the right think for this situation… because it didn’t work, and it seems it is more use if ListA was empty (which I don’t care about)

  • 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-15T04:46:18+00:00Added an answer on June 15, 2026 at 4:46 am

    You can modify your last line of code as follows:

    Selected = (ListA.FirstOrDefault(x => x.ID == item.ID) ?? item).Selected;
    

    For DefaultIfEmpty, you’d have to do

    Selected = ListA.Where(x => x.ID == item.ID).DefaultIfEmpty(item)
                                                           .First().Selected;
    

    which is harder to follow but does essentially the same thing.

    As Dominic has noted below, if you use SingleOrDefault/Single in place of FirstOrDefault/First, you will get an exception if you find two items in ListA with the same ID – which may be a check you want to introduce.

    (There are better/more efficient ways of doing this, but as you say, you want to fix this way of doing it, not use a totally different way.)

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

Sidebar

Related Questions

This question is hard to phrase, so I'm going to have to use some
I'm just testing out some basic networking code written in Visual C++. I have
I need some basic tutorial or guide on how to use PhysX™ Candy Wrapper.
I have an opportunity to learn some new tech to write this code. Basic
I'm going to use the basic version of the Blueimp jQuery-File-Upload library for file
I'm relatively new to cross-platform mobile development, and recently have been going through some
I have some basic knowledge about compiling C but need to get a couple
Can anyone give me some basic directions to keep me going? My general requirements
im going to use Sociallib library in my app to interact with social networks.
I'm going to use Codeigniter's session data for my login system, but first I

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.