查询。从表格中选择对象的数量> N

时间:2018-05-14 15:40:24

标签: c# linq-to-sql

我的表格“汽车”包含字段 ModelName,Maker,Year,Price 和表格制作者“字段名称。 我需要Linq到Sql查询,我可以选择所有制造商的模型数量> 名词的。

我尝试过:

Data.ItemsSource = (from m in db.GetTable<Maker>()
                    where (from c in db.GetTable<Car>() 
                           where c.Maker == m.Name 
                           select c).Count() > 1
                    select m.Name);

但没有结果。我该如何实现呢?

UPD。 表车代码

CREATE TABLE [dbo].[Car] (
    [CarId]     INT           IDENTITY (1, 1) NOT NULL,
    [ModelName] NVARCHAR (50) NOT NULL,
    [Equipment] NVARCHAR (50) NULL,
    [Year]      INT           NULL,
    [Price]     INT           NULL,
    [Cathegory] NCHAR (20)    DEFAULT ('Sedan') NOT NULL,
    [Maker]     NCHAR (20)    DEFAULT ('Toyota') NOT NULL,
    PRIMARY KEY CLUSTERED ([CarId] ASC),
    CONSTRAINT [FK_Car_ToCatheg] FOREIGN KEY ([Cathegory]) REFERENCES [dbo].[Catheg] ([Name]) ON DELETE CASCADE,
    CONSTRAINT [FK_Car_ToMaker] FOREIGN KEY ([Maker]) REFERENCES [dbo].[Maker] ([Name]) ON DELETE CASCADE
);

Class Car

 [Table(Name = "Car")]
    public class Car
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        public int CarId { get; set; }
        [Column]
        public string ModelName { get; set; }
        [Column]
        public string Equipment { get; set; }
        [Column]
        public int Year { get; set; }
        [Column]
        public int Price { get; set; }
        [Column]
        public string Maker { get; set; }
        [Column]
        public string Cathegory { get; set; }

        public Car()
        { }
        public Car(string modelName, string equipment, int year, int price, string cath, string carmaker, int id = -1)
        {
            ModelName = modelName;
            Equipment = equipment;
            Year = year;
            Price = price;
            Maker = carmaker;
            Cathegory = cath;
            CarId = id;
        }

        public override string ToString()
        {
            return ModelName + " " + Equipment + " " + Year + " " + Maker + " " + Price + " " + Cathegory;

        }

表制作代码

CREATE TABLE [dbo].[Maker] (
    [Name] NCHAR (20) NOT NULL,
    PRIMARY KEY CLUSTERED ([Name] ASC)
);

Class Maker

[Table(Name = "Maker")]
public class Maker
{
    [Column(IsPrimaryKey = true)]
    public string Name { get; set; }

}

2 个答案:

答案 0 :(得分:0)

这应该为您提供各自计数的制造商名称。

var Results = from c in db.GetTable<Car>() 
group c by c.Maker into cg
join m in db.GetTable<Maker>() on cg.FirstOrDefault().Maker equals m.Name
where cg.Count() > 1
select new 
{
    MakerName = m.Name,
    Count = cg.Count()
};

答案 1 :(得分:0)

如果您实际上意味着每个Cars Maker.Name 的数量,则可以只对Car.Maker进行分组,并且Maker表不需要:

Data.ItemsSource = from c in db.GetTable<Car>()
                   group c by c.Maker into cg
                   where cg.Count() > 1
                   select cg.Key;

如果模型实际上与YearPrice无关,那么您只需计算ModelName s:

var models = (from c in db.GetTable<Car>()
             select new { c.Maker, c.ModelName }).Distinct();

Data.ItemsSource = from c in models
                   group c by c.Maker into cg
                   where cg.Count() > 1
                   select cg.Key;