LINQ的C#模型基础结构

时间:2019-01-10 20:29:39

标签: c# linq

我想了解为什么要在模型结构中调用两个表的正确方法。让我们将表称为carid,carmodel和carmanufacturersid的汽车。下表中有制造商,具有制造商ID和制造商名称。因此,我喜欢提取车型和制造商名称。因此,在汽车模型中,我要为公共IEnumerable厂商名称(get;组; }?

     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;
            }
        }

1 个答案:

答案 0 :(得分:0)

这是一个简单的实现。您还可以修改GetAllCars()查询的select语句,以返回包含所需DB字段子集的动态对象。此外,Here is a useful link用于出于各种目的创建Linq查询,

我为广泛的模拟DB样板致歉...

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;
        }
    }
}