如何在select子句中创建匿名类型列表?

时间:2011-03-24 17:26:34

标签: sql linq

以下是我在项目中使用的查询之一:



var carQuery = from cars in context.Cars
                           .Where(c => c.CarID==3)
                           from stockTypes in context.StockTypes
                            .Where(st => cars.StockTypeId == st.StockTypeID).DefaultIfEmpty()
                           from carUnit in context.Car_Units
                           .Where(cu => cu.CarId == cars.CarID).DefaultIfEmpty()
                           from carAttributes in context.Car_Attributes
                           .Where(ca => ca.CarId == cars.CarID).DefaultIfEmpty()
                           from attribute in context.Attributes
                           .Where(attr => attr.AttributeId==carAttributes.AttributeId).DefaultIfEmpty()

                           select new
                           {
                               CarID = cars.CarID,
                               CarName = cars.CarName,
                               CarDescription = cars.CarDescription,
                               StockType = (stockTypes == null) ? null : new
                               {
                                   StockTypeID = stockTypes.StockTypeID,
                                   StockName = stockTypes.StockName
                               },
                               IsActive = cars.IsActive,
                               IsCab = cars.IsCab,
                               Unit = (carUnit == null) ? null : new
                               {
                                   Id = carUnit.UnitId,
                                   Name = carUnit.Unit.UnitName
                               },
                               Attributes = attribute
                           };

如果context.Attributes返回多行,则整个结果集也会返回多行。

是否有可能将具有多个属性的单一车型作为车辆属性列表返回?

请帮忙。

谢谢, 马赫什

1 个答案:

答案 0 :(得分:0)

您只需要在结果集中移动属性查询:

        var carQuery = from cars in context.Cars
                       .Where(c => c.CarID==3)
                       from stockTypes in context.StockTypes
                        .Where(st => cars.StockTypeId == st.StockTypeID).DefaultIfEmpty()
                       from carUnit in context.Car_Units
                       .Where(cu => cu.CarId == cars.CarID).DefaultIfEmpty()
                       from carAttributes in context.Car_Attributes
                       .Where(ca => ca.CarId == cars.CarID).DefaultIfEmpty()

                       select new
                       {
                           CarID = cars.CarID,
                           CarName = cars.CarName,
                           CarDescription = cars.CarDescription,
                           StockType = (stockTypes == null) ? null : new
                           {
                               StockTypeID = stockTypes.StockTypeID,
                               StockName = stockTypes.StockName
                           },
                           IsActive = cars.IsActive,
                           IsCab = cars.IsCab,
                           Unit = (carUnit == null) ? null : new
                           {
                               Id = carUnit.UnitId,
                               Name = carUnit.Unit.UnitName
                           },
                           Attributes = 
                              from attribute in context.Attributes
                              .Where(attr => attr.AttributeId==carAttributes.AttributeId).DefaultIfEmpty()
                              .GroupBy(at => at.AttributeId)
                              select attribute
                       };