Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6342153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:11:02+00:00 2026-05-24T20:11:02+00:00

I have a linq query that works just fine but when StyleCop is run

  • 0

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)outletoffroadagent.Id,
                    AgentType = outlettype_4.Value,
                    Email = outletcommunicationmethod_4.Value,
                    BondExpirationDate = (DateTime)outletoffroadagent.BondExpirationDate
                }
                );

           return query.ToList();
       }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:11:03+00:00Added an answer on May 24, 2026 at 8:11 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this linq query that works well (although it may be written better,
I have a LINQ to ENTITY query that pulls from a table, but I
I have a Linq query which is working fine but i have noticed when
I have a query that works perfectly in SQL, but I'm having the damnedest
I have a fairly long linq query and everything works as it should.. but
I have a Linq query that looks something like this: var myPosse = from
I have a Linq query that looks something like this: var query = from
I have a LINQ query that returns some object like this... var query =
I have a Linq query that returns an object of the type IQueryable<ClassX> .
I have an anonymous linq query that I bind to a datagrid, when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.