开发者

Working linq query produces the error "CA1505 : Microsoft.Maintainability"

I have a linq query that works just fine but when StyleCop is run on the the project the following error is produced: "CA1505 : Microsoft.Maintainability: Rewrite or refactor the type's methods to increase its maintainability index (MI)". I have tested removing part of the query and found that that fixes the error. The issue appears to be that the query is just too long, so I'm looking for a way to shorten it to eliminate the CA1505 error. The challenge here is that the data resides in 5 main tables, the sql equivalent to this is basically 5 union all statements. Any Suggestions?

    public List<ExpiringItemModel> GetByDaysToExpire(int days)
    {
        var int1 = days - 30;
        var int2 = days - 16;
        var int3 = days - 0;

        var query = (
            from outlet in _modelContext.Outlets
            join outletcommunicationmethod in _modelContext.OutletCommunicationMethods on
                new { Id = outlet.Id }
                equals new { Id = outletcommunicationmethod.OutletId }
            join outletcarpermitagent in _modelContext.outletcarpermitagents on
                new { Id = outlet.Id } equals
                new { Id = outletcarpermitagent.OutletId }
            join outlettype in _modelContext.OutletTypes on
                new { OutletTypeId = outletcarpermitagent.OutletTypeId } equals
                new { OutletTypeId = outlettype.Id }
            where
                (outletcommunicationmethod.CommunicationMethodId == 1
                &&
                SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                      outletcarpermitagent.BondExpirationDate) > 0) &&

                (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                        outletcarpermitagent.BondExpirationDate) == days ||
                (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                        outletcarpermitagent.BondExpirationDate) == 30) ||
                (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                        outletcarpermitagent.BondExpirationDate) == 14))

            select new ExpiringItemModel
            {
                Id = (Int32)outletcarpermitagent.Id,
                AgentType = outlettype.Value,
                Email = outletcommunicationmethod.Value,
                BondExpirationDate = (DateTime)outletcarpermitagent.BondExpirationDate
            }
            ).Concat
            (
                from outlet_1 in _modelContext.Outlets
                join outletcommunicationmethod_1 in _modelContext.OutletCommunicationMethods on
                    new { Id = outlet_1.Id } equals new { Id = outletcommunicationmethod_1.OutletId }
                join outlettruckpermitagent in _modelContext.outlettruckpermitagents on
                    new { Id = outlet_1.Id } equals new { Id = outlettruckpermitagent.OutletId }
                join outlettype_1 in _modelContext.OutletTypes on
                    new { OutletTypeId = outlettruckpermitagent.OutletTypeId } equals
                    new { OutletTypeId = outlettype_1.Id }
                where
                    (outletcommunicationmethod_1.CommunicationMethodId == 1
                    &&
                    SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                          outlettruckpermitagent.BondExpirationDate) > 0) &&

                    (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                          outlettruckpermitagent.BondExpirationDate) == days ||
                    (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                            outlettruckpermitagent.BondExpirationDate) == 30) ||
                    (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                            outlettruckpermitagent.BondExpirationDate) == 14))

                select new ExpiringItemModel
                {
                    Id = (Int32)outlettruckpermitagent.Id,
                    AgentType = outlettype_1.Value,
                    Email = outletcommunicationmethod_1.Value,
                    BondExpirationDate = (DateTime)outlettruckpermitagent.BondExpirationDate
                }
            ).Concat
            (
                from outlet_2 in _modelContext.Outlets
                join outletcommunicationmethod_2 in _modelContext.OutletCommunicationMethods on
                    new { Id = outlet_2.Id } equals new { Id = outletcommunicationmethod_2.OutletId }
                join outletcyclepermitagent in _modelContext.outletcyclepermitagents on new { Id = outlet_2.Id } equals
                    new { Id = outletcyclepermitagent.OutletId }
                join outlettype_2 in _modelContext.OutletTypes on new { OutletTypeId = outletcyclepermitagent.OutletTypeId }
                    equals new { OutletTypeId = outlettype_2.Id }
                where
                    (outletcommunicationmethod_2.CommunicationMethodId == 1
                    &&
                    SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                          outletcyclepermitagent.BondExpirationDate) > 0) &&

                    (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                          outletcyclepermitagent.BondExpirationDate) == days ||
                    (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                            outletcyclepermitagent.BondExpirationDate) == 30) ||
                    (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                            outletcyclepermitagent.BondExpirationDate) == 14))

                select new ExpiringItemModel
                {
                    Id = (Int32)outletcyclepermitagent.Id,
                    AgentType = outlettype_2.Value,
                    Email = outletcommunicationmethod_2.Value,
                    BondExpirationDate = (DateTime)outletcyclepermitagent.BondExpirationDate
                }
                ).Concat
                (
                from outlet_3 in _modelContext.Outlets
                join outletcommunicationmethod_3 in _modelContext.OutletCommunicationMethods on
                    new { Id = outlet_3.Id } equals new { Id = outletcommunicationmethod_3.OutletId }
                join outletscooteragent in _modelContext.outletscooteragentAgents on
                    new { Id = outlet_3.Id } equals
                    new { Id = outletscooteragent.OutletId }
                join outlettype_3 in _modelContext.OutletTypes on
                    new { OutletTypeId = outletscooteragent.OutletTypeId }
                    equals new { OutletTypeId = outlettype_3.Id }
                where
                    (outletcommunicationmethod_3.CommunicationMethodId == 1
                    &&
                    SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                          outletscooteragent.BondExpirationDate) > 0) &&

                    (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                          outletscooteragent.BondExpirationDate) == days ||
                    (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                            outletscooteragent.BondExpirationDate) == 30) ||
                    (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                            outletscooteragent.BondExpirationDate) == 14))

                select new ExpiringItemModel
                {
                    Id = (Int32)outletscooteragent.Id,
                    AgentType = outlettype_3.Value,
                    Email = outletcommunicationmethod_3.Value,
                    BondExpirationDate = (DateTime)outletscooteragent.BondExpirationDate
                }
                ).Concat
                (
                from outlet_4 in _modelContext.Outlets
                join outletcommunicationmethod_4 in _modelContext.OutletCommunicationMethods on
                    new { Id = outlet_4.Id } equals new { Id = outletcommunicationmethod_4.OutletId }
                join outletoffroadagent in _modelContext.outletoffroadagentAgents on new { Id = outlet_4.Id } equals
                    new { Id = outletoffroadagent.OutletId }
                join outlettype_4 in _modelContext.OutletTypes on new { OutletTypeId = outletoffroadagent.OutletTypeId }
                    equals new { OutletTypeId = outlettype_4.Id }
                where
                    (outletcommunicationmethod_4.CommunicationMethodId == 1
                    &&
                    SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                          outletoffroadagent.BondExpirationDate) > 0) &&

                    (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                             outletoffroadagent.BondExpirationDate) == days ||
                    (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                            outletoffroadagent.BondExpirationDate) == 30) ||
                    (SqlFunctions.DateDiff("day", SqlFunctions.GetDate(),
                                            outletoffroadagent.BondExpirationDate) == 14))

                select new ExpiringItemModel
                {
                    Id = (Int32)outletoffro开发者_开发知识库adagent.Id,
                    AgentType = outlettype_4.Value,
                    Email = outletcommunicationmethod_4.Value,
                    BondExpirationDate = (DateTime)outletoffroadagent.BondExpirationDate
                }
                );

           return query.ToList();
       }


You could split out each Concat into its own query.

For example

var query1 = from outlet in _modelContext.Outlets
              ...;

var query2 = from outlet in _modelContext.Outlets
              ...;


var fullquery = query1.Concat(query2).Concat(...);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜