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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:21:15+00:00 2026-05-30T21:21:15+00:00

I had to write a method that does the following: There is a DataSet

  • 0

I had to write a method that does the following:
There is a DataSet let’s say CarDataSet with one table Car and contains Primary key Id and one more column ColorId. And there is a string with Ids seperated with commas for example “5,6,7,8” (random length). The task is to check if all appropriate ColorIds are identical for given Car Ids.

For example:
String ids = "5,6,7,8"
If all the Cars ColorIds are for example 3,3,3,3 where the Car Ids are 5,6,7,8 then return true;

In other words – check if all cars with given Ids are in one color. Now I don’t have my code anymore but I made this using 3 foreach loops and 3 linq expressions. Is there any simplier way to do this?

  • 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-30T21:21:16+00:00Added an answer on May 30, 2026 at 9:21 pm

    If you want all cars have same color means all of them should have same color as first one:

    // first find the cars with given ids
    var selectedCars = Cars.Where(x=>ids.Contains(x.ID.ToString());
    // select one of them as comparer:
    var firstCar = selectedCars.FirstOrDefault();
    if (firstCar == null)
       return true;
    // check all of them has same color as first one:
    return selectedCars.All(x=>x.ColorID == firstCar.ColorID);
    

    Edit: Or if you have no problem with throwing exception when there is no car with given ids you can use two query in lambda syntax:

    var selectedCars = Cars.Where(x=>ids.Contains(x.ID.ToString()));
    return selectedCars.All(x=>x.ColorID == selectedCars.First().ColorID);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If you had to write code that takes messages from a message queue and
I recently submitted a small program for an assignment that had the following two
This one had me going crazy for awhile. I have an app that was
Is there some method where one could use a try statement to catch an
Have had to write my first proper multithreaded coded recently, and realised just how
I've just had to write a string reverse function in C# 2.0 (i.e. LINQ
I have experience with OCaml. You had to write a stub for every function
I had a task to write simple game simulating two players picking up 1-3
I had a task to write simple game simulating two players picking up 1-3
My kiddo had a homework assignment to write Blackjack in Java. I helped him

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.