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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:41:26+00:00 2026-06-13T05:41:26+00:00

I have a LINQ class of this format in C#: class Vehicle { int

  • 0

I have a LINQ class of this format in C#:

class Vehicle
{
  int _VehicleID
  int _ModelID

  EntetySet<Cars> _AllCars
  EntetySet<Bus> _AllBus
  EntetyRef<Driver> _Person
}

Cars table

CarsID | Manufacturer | Type
1000   | Honda        | Diesel
1001   | Mitsubishi   | Petrol
1002   | Maruti       | Diesel

Bus table

BusID | Manufacturer | Type
2000  | Volvo        | Diesel
2001  | TATA         | Petrol
2002  | layland      | Petrol

From the UI, I will get a Vehicle ID as a parameter. Based on that, all the cars, Bus and its associated tables will get copied to a variable. That is:

Vehicle v1 = new Vehicle();
v1 = dc.vehicle.where(v => v.vehicleID == param.vehicleID).findfirst();

My v1 has all the table and its contents which satisfies the above condition.

Now, I want to filter some more based on the table content present in table cars and bus; i.e based on type of vehicle petrol or diesel.

If I want to copy all the petrol cars and Buses using a query statement in a single line, please tell me the way.

Thanks in advance.

  • 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-13T05:41:27+00:00Added an answer on June 13, 2026 at 5:41 am

    ** Edit based upon comments **

    The logic of your question somehow slipped by me, because I was taking a logical Human approach on vehicle. Whenever I look at this Vehicle class I instantly see cars and busses as vehicles to.
    This indicates more that the naming is poorly chosen.
    But no more judging from me.

    I understand you want one vehicle by id and you want the result to only contain cars and busses riding on let’s say Petrol, in a single line.
    If it is translated to SQL than I don’t know how far the tree can go within a select, but if it is in memory than you can go a long way inside a subselect.

    You could use a ‘Model’ class to achieve that or you could go with an anonymous type, but I will give an example for a ‘Model’ class:

    Example:

    public class VehicleModel
    {
        public int VehicleID { get; set; }
        public int ModelID { get; set; }
        public List<Cars> Cars { get; set; }
        public List<Bus> Buses { get; set; }
    }
    
    var vehicleID = 1; // just for the example of course.
    var fuelType = "Petrol";
    var vehicle = (from v in dc.vehicle 
                   where v._VehicleID == vehicleID
                   select new VehicleModel
                   {
                       VehicleID = v._VehicleID,
                       ModelID = v._ModelID,
                       Cars = v._AllCars.Where(car => car.Type == fuelType).ToList(),
                       Buses = v._AllBus.Where(bus => bus.Type == fuelType).ToList()
                   }).SingleOrDefault();
                   // use FirstOrDefault() when _VehicleID is NOT unique to get TOP 1
    

    Do not modify the EntitySets within the Entity itself, always use a model for these things, because if you would do that and call save changes on your EntityContainer all sorts of things could go wrong.

    If you want to know about anonymous types take a look here:
    http://msdn.microsoft.com/en-us/library/bb397696(v=vs.100).aspx

    Also take a look here for Linq examples:
    http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

    – before edit

    Do you mean this:

    var query = dc.vehicle.Where(v => 
                                 v._AllCars.Any(c => c.Manufacturer == "Honda") ||
                                 v._AllBuss.Any(b => b.Manufacturer == "Volvo"));
    

    This will give you all vehicles where the cars are Honda Or the Busses are Volvo.
    Which will be:

    1000 | Honda | Diesel
    2000 | Volvo | Diesel

    the result is an IEnumerable containing all or no items satisfying the criteria.

    If you only want the first hit you can do:

    //if it must exist, otherwise this throws an exception
    var firstVehicle = query.First();
    //if it may exist, otherwise null
    var firstVehicle = query.FirstOrDefault();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of this object: public class Customer { public int Id
I have a LINQ to SQL class VoucherRecord based on a simple table. One
I have a Linq-To-Sql based repository class which I have been successfully using. I
I have a class containing Linq To SQL objects that are used to populate
I have a class which provides generic access to LINQ to SQL entities, for
I have a bunch of entity class (generated by Linq to SQL) with a
I have a BankAccount table. LINQ to SQL generates a class named BankAccount as
I have linq request. I need get item.Title in select. how do this? var
I'm have .dbml Linq to SQL class named DExamination.dbml [global::System.Data.Linq.Mapping.TableAttribute(Name=dbo.Examination)] public partial class Examination
I have a string array in this format (each line is a slot in

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.