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

  • SEARCH
  • Home
  • 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 6112763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:46:56+00:00 2026-05-23T14:46:56+00:00

I have the following controller code that returns a Json list object to my

  • 0

I have the following controller code that returns a Json list object to my view that draws a pie chart.

There are 4 input parameters and i have it working with 3 of them.

However, the fist parameter entitled ‘SiteTypeId’ needs to be included in the where.

My problem is how to include this neatly in the code, i’d like to avoid an override of the function.

The required additional logic is:

  1. if SiteTypeId = -1 (then this means show all so nothing is to be changed)
  2. if SiteTypeId = 0 (then i.SiteTypeId == 0 needs to be added)
  3. if SiteTypeId = 1 (then i.SiteTypeId == 1 needs to be added)

If 2 and 3 above were all that was required it would be easy I guess. I’m thinking there must be a neat expression for this or a neat way of splitting the LINQ into 2 with a condition perhaps.

I’m new to LINQ – can anyone advise me, here is the controller code i need to modify:

    public JsonResult GetChartData_IncidentsBySiteStatus(string SiteTypeId, string searchTextSite, string StartDate, string EndDate)
    {
        if (searchTextSite == null)
            searchTextSite = "";

        DateTime startDate = DateTime.Parse(StartDate);
        DateTime endDate = DateTime.Parse(EndDate);

        var qry = from s in _db.Sites   
                  join i in _db.Incidents on s.SiteId equals i.SiteId
                  where s.SiteDescription.Contains(searchTextSite)
                    && (i.Entered >= startDate && i.Entered <= endDate)
                  group s by s.SiteStatus.SiteStatusDescription + "[" + s.SiteTypeId.ToString() + "]"
                      into grp
                      select new
                      {
                          Site = grp.Key,
                          Count = grp.Count()
                      };

        return Json(qry.ToList()  , JsonRequestBehavior.AllowGet);
    }
  • 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-23T14:46:56+00:00Added an answer on May 23, 2026 at 2:46 pm

    Try this:

    public JsonResult GetChartData_IncidentsBySiteStatus(string SiteTypeId, string searchTextSite, string StartDate, string EndDate)
    {
            if (searchTextSite == null)
                    searchTextSite = "";
    
            DateTime startDate = DateTime.Parse(StartDate);
            DateTime endDate = DateTime.Parse(EndDate);
    
            var  incidentsQry = _db.Incidents;
            if(SiteTypeId > -1)
            {
                incidentsQry = incidentsQry.Where(a=>a.SiteTypeId == SiteTypeId);
            }
    
            var qry = from s in _db.Sites   
                                join i in incidentsQry  on s.SiteId equals i.SiteId
                                where s.SiteDescription.Contains(searchTextSite)
                                    && (i.Entered >= startDate && i.Entered <= endDate)
                                group s by s.SiteStatus.SiteStatusDescription + "[" + s.SiteTypeId.ToString() + "]"
                                        into grp
                                        select new
                                        {
                                                Site = grp.Key,
                                                Count = grp.Count()
                                        };
    
            return Json(qry.ToList()  , JsonRequestBehavior.AllowGet);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following code in my controller: public ActionResult Details(int id) { var
I have a controller with the following action code: public JsonResult CheckPasswordStrength(string password, string
I have the following code that i am trying to change from a regular
I have the following code that creates two objects (ProfileManager and EmployerManager) where the
My view controller is not responding to didRotateFromInterfaceOrientation , despite that I have added
I have a controller action UpdateCustomer(CustomerDto customer) that returns a PartialViewResult with a model
I have the following controller action and test. I'm new to testing with Shoulda
I have the following route defined routes.MapRoute( ItemName, {controller}/{action}/{projectName}/{name}, new { controller = Home,
I have the following in my Global.asax.cs routes.MapRoute( Arrival, {partnerID}, new { controller =

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.