-2

I wanted to understand the correct why to call two table in a model structure. Lets call table one car with carid, carmodel and carmanufacturersid. The next table we have manufacturers that has manufacturersid and manufacturersname. So I like to pull out carmodel and manufacturersname. So in the model for car do I add the model for manufacturers like public IEnumerable manufacturersname { get; set; }?

     public class car
        {
            public int carId { get; set; }
            public string carmodel { get; set; }
            public int carmanufacturersid { get; set; }
            //public IEnumerable<manufacturer> manufacturersname { get; set; }
            public Model.manufacturer manufacturername{ get; set; }
        }

 public class manufacturer
    {
        public int manufacturerId { get; set; }
        public string manufacturername{ get; set; }
    }


        public List<Model.car> GetAllCars()
        {
            using (var db = new Entities())
            {
                var cars = (from c in db.car
                                     join m in db.manufacturersId  on m.manufacturersId id equals c.manufacturersId
                                     select new Model.car
                                      {
                                            carmodel = c.carmodel,
                                            manufacturersname = m.manufacturersname

                                     }
                                    ).ToList();



                return cars;
            }
        }
Jefferson
  • 173
  • 2
  • 12
  • 32
  • 3
    Name your types using singular instead of plural, `class Car` not `class Cars`. Then the relationship should become apparent (hint: does 1 car have multiple manufacturers?) – Igor Jan 10 '19 at 20:34
  • hmmm not sure what you mean? – Jefferson Jan 10 '19 at 20:48
  • `public IEnumerable manufacturersname { get; set; }` <= does that belong? If it does how would that work? Why would you want multiple manufacturers on a single car instance? – Igor Jan 10 '19 at 20:49
  • Also `List` is not compatible with `Model.Reimbursement`, that method `GetAllCars()` should not compile with the code shown based only on the return types. – Igor Jan 10 '19 at 20:51
  • how would I return manufacturersname if I dont add it to the model for cars? – Jefferson Jan 10 '19 at 20:53
  • [what is IEnumerable in .net](https://stackoverflow.com/questions/3014737/what-is-ienumerable-in-net) – Igor Jan 10 '19 at 20:54

1 Answers1

0

Here is a simplistic implementation. You can also modify the select statement of the GetAllCars() query to return a dynamic object containing a desired subset of DB fields. Additionally, Here is a useful link for creating Linq queries for a variety of purposes that might be useful.

I apologize for the extensive mock DB boilerplate...

using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharpPractice.StackOverflow
{
    public class Manufacturer
    {
        public int Id      { get; set; }
        public string Name { get; set; } // "Toyota"
    }

    public class Model
    {
        public int Id             { get; set; }
        public int ManufacturerFK { get; set; }
        public string Name        { get; set; } // "Land Cruiser"
        public int Year           { get; set; }
    }

    public class Car
    {
        public int Id       { get; set; }
        public int ModelFK  { get; set; }
        public string Color { get; set; }
    }

    public class StaticCarDB
    {
        static List<Manufacturer> ManufacturerTable = new List<Manufacturer>();
        static List<Model>        ModelTable        = new List<Model>();
        static List<Car>          CarTable          = new List<Car>();

        static StaticCarDB()
        {
            ManufacturerTable.Add( new Manufacturer() { Id = 0, Name = "Audi" });
            ManufacturerTable.Add( new Manufacturer() { Id = 1, Name = "BMW"  });

            ModelTable.Add(new Model() { Id = 0, ManufacturerFK = 0, Name = "A4",   Year = 2007 });
            ModelTable.Add(new Model() { Id = 1, ManufacturerFK = 0, Name = "A6",   Year = 2006 });
            ModelTable.Add(new Model() { Id = 2, ManufacturerFK = 1, Name = "325i", Year = 1990 });
            ModelTable.Add(new Model() { Id = 3, ManufacturerFK = 1, Name = "525",  Year = 2001 });

            CarTable.Add(new Car() { Id = 0, ModelFK = 0, Color = "Metallic Sand Micah" });
            CarTable.Add(new Car() { Id = 1, ModelFK = 2, Color = "Black" });
            CarTable.Add(new Car() { Id = 2, ModelFK = 2, Color = "Green" });
            CarTable.Add(new Car() { Id = 3, ModelFK = 3, Color = "Black" });
        }

        public static IEnumerable<Tuple<Car, Model, Manufacturer>> GetAllCars()
        {
            var cars =
                from car in CarTable
                from model in ModelTable
                    where car.ModelFK == model.Id
                from manufacturer in ManufacturerTable
                    where model.ManufacturerFK == manufacturer.Id
                select new Tuple<Car, Model, Manufacturer>(car, model, manufacturer);

            return cars;
        }
    }
}
TheFastCat
  • 3,134
  • 4
  • 22
  • 32