简体   繁体   English

仅选择具有多个包含的 mvc linq 查询中的某些字段

[英]select only certain fields in an mvc linq query that has multiple includes

When looking at generated json,I saw that a rules page was including image data.在查看生成的 json 时,我看到一个规则页面包含图像数据。 I traced it to this code: `我将其追溯到此代码:`

.Include(m => m.OrderRuleType)
            .Include(m => m.OrderRuleType.Type)
            .Include(m => m.OrderRuleType.Category)
            // best match fields
            .Include(m => m.TicketType)
            .Include(m => m.DestTicketType)
            .Include(m => m.Shipper)
            .Include(m => m.CarrierType)
            .Include(m => m.Carrier)
            .Include(m => m.DriverGroup)
            .Include(m => m.Driver)
            .Include(m => m.TruckType)
            .Include(m => m.ProductGroup)
            .Include(m => m.Destination)
            .Include(m => m.Origin)
            .Include(m => m.DestState)
            .Include(m => m.OriginState)
            .Include(m => m.DestRegion)
            .Include(m => m.OriginRegion)
            .Include(m => m.Producer)
        .Where(m => !m.OrderRuleType.Hidden)
        .Where(m => m.ID == id || id == 0)

I tried doing a .select on the offending include (shipper) like this:我尝试对违规包含(托运人)执行 .select ,如下所示:

.Select(m => new { m.ID, m.Name}})

that caused a red line for all code after it.这导致其后的所有代码都出现红线。 I tried putting an .asEnumerable() on it as well.我也尝试将 .asEnumerable() 放在上面。

What is the proper way to select just multiple fields from these?从这些字段中选择多个字段的正确方法是什么? Pretty much all of them we are only using the ID and name field from that table.几乎所有这些我们只使用该表中的 ID 和名称字段。

I could probably do a .select at the very end, but i do not know if it's possible to select out values like that without creating a view model.我可能会在最后做一个 .select,但我不知道是否可以在不创建视图模型的情况下选择这样的值。 I tried adding a select after the shipper include but it broke all following includes.我尝试在托运人包含之后添加一个选择,但它破坏了所有以下包含。

I solved this by using an anonymous select.我通过使用匿名选择解决了这个问题。 I was not able to select directly into the model for two reasons: 1. Some of the fields (all the named ones) were notmapped fields without setters, and 2. The model was bound to a table.由于两个原因,我无法直接选择到模型中:1. 一些字段(所有命名的)没有没有 setter 的未映射字段,以及 2. 模型绑定到一个表。 Here is working code:这是工作代码:

var data = db.OrderRules
                        .Include(m => m.OrderRuleType)
                        .Include(m => m.OrderRuleType.Type)
                        .Include(m => m.OrderRuleType.Category)
                        // best match fields
                        .Include(m => m.TicketType)
                        .Include(m => m.DestTicketType)
                        .Include(m => m.Shipper)
                        .Include(m => m.CarrierType)
                        .Include(m => m.Carrier)
                        .Include(m => m.DriverGroup)
                        .Include(m => m.Driver)
                        .Include(m => m.TruckType)
                        .Include(m => m.ProductGroup)
                        .Include(m => m.Destination)
                        .Include(m => m.Origin)
                        .Include(m => m.DestState)
                        .Include(m => m.OriginState)
                        .Include(m => m.DestRegion)
                        .Include(m => m.OriginRegion)
                        .Include(m => m.Producer)
                    .Where(m => !m.OrderRuleType.Hidden)
                    .Where(m => m.ID == id || id == 0)
                    .Select(m => new { 
                        m.ID,
                        allowRowDeactivate = true,
                        allowRowEdit = true,
                        m.EffectiveDate,
                        m.EndDate,
                        m.Value,
                        m.TypeID,
                        m.OrderRuleType,
                        m.OrderRuleType.Type,
                        m.OrderRuleType.Category,
                        OriginTicketTypeName = m.TicketType.Name ?? BestMatchCore.DEFAULT_LABEL,
                        DestTicketTypeName = m.DestTicketType.Name ?? BestMatchCore.DEFAULT_LABEL,
                        ShipperName = m.Shipper.Name ?? BestMatchCore.DEFAULT_LABEL,
                        CarrierTypeName = m.CarrierType.Name ?? BestMatchCore.DEFAULT_LABEL,
                        CarrierName = m.Carrier.Name ?? BestMatchCore.DEFAULT_LABEL,
                        DriverGroupName = m.DriverGroup.Name ?? BestMatchCore.DEFAULT_LABEL,
                        DriverName = (m.Driver.FirstName + " " + m.Driver.LastName == " ")? BestMatchCore.DEFAULT_LABEL: m.Driver.FirstName + " " + m.Driver.LastName,
                        TruckTypeName = m.TruckType.Name ?? BestMatchCore.DEFAULT_LABEL,
                        ProductGroupName = m.ProductGroup.Name ?? BestMatchCore.DEFAULT_LABEL,
                        DestinationName = m.Destination.Name ?? BestMatchCore.DEFAULT_LABEL,
                        OriginName = m.Origin.Name ?? BestMatchCore.DEFAULT_LABEL,
                        DestStateName = m.DestState.Abbreviation ?? BestMatchCore.DEFAULT_LABEL,
                        OriginStateName = m.OriginState.Abbreviation ?? BestMatchCore.DEFAULT_LABEL,
                        DestRegionName = m.DestRegion.Name ?? BestMatchCore.DEFAULT_LABEL,
                        OriginRegionName = m.OriginRegion.Name ?? BestMatchCore.DEFAULT_LABEL,
                        ProducerName = m.Producer.Name ?? BestMatchCore.DEFAULT_LABEL,
                        CreateDate = m.CreateDateUTC,
                        m.CreatedByUser,
                        LastChangeDate = m.LastChangeDateUTC,
                        m.LastChangedByUser

                        });

This created a query that only brought back the fields i wanted.这创建了一个只带回我想要的字段的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM