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

发布于 2024-10-26 11:02:03 字数 1873 浏览 1 评论 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()
                           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 返回多行,则整个结果集也返回多行。

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

请帮忙。

谢谢, 马赫什

Here is one of queries I am using in my project:



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

If the context.Attributes returns multiple rows, the whole resultset also returning multiple rows.

Is there any possibility to return a single car type with multiple attributes as a list of attributes to the car??

Please help.

Thanks,
Mahesh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最后的乘客 2024-11-02 11:02:03

您只需将属性查询移动到结果集中:

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

You just need to move the attribute query inside the result set:

        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
                       };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文