How to translate SQL to C# - c#

I have a SQL query that looks like this:
SELECT
a.Date,
CASE
WHEN a.Type = 'String 1' OR a.Type = 'String 2'
THEN 'foo'
ELSE 'bar'
END AS VisitType,
DATEDIFF (d, (SELECT TOP 1 Date FROM dim.Date WHERE DateKey = a.StartDate),
(SELECT TOP 1 Date FROM dim.Date WHERE DateKey = a.EndDate)) AS Duration
I am trying to convert it to a C# expression and so far I have something like this:
var allowedTypes = new[]{"String 1","String 2", "String 3", "String 4"}
var Data = from a in dbContext.Claim
where a.MemberId = memberId
&& a.StartDate > startDate
&& a.EndDate <= endDate
&& a.Type.Where(???t => allowedTypes.Contains(allowedTypes)) // This line I have issues with
select new
{
Date = a.EndDate,
VisitType = ???,
VisitDuration = ???
}
I am having difficulty with the DateDiff concepts and doing a Contains like method with a string array.
ALSO I have realized that the type of the dates are contained in a nullable int.
Thanks for all your advice so far~!

Try moving the conditional into the result:
select new
{
Date = a.EndDate,
VisitType = allowedTypes.Contains(a.Type) ? "foo" : "bar",
VisitDuration = ???
}

var result = dbContext.Claims
.Where (claim => claim.MemberId = memberId
&& claim.StartDate > startDate
&& claim.EndDate <= endDate
.Select(claim => new
{
Date = claim.EndDate,
VisitType = allowedTypes.Contains(claim.Type) ? "foo" : "bar",
VisitDuration = claim.EndDate - claim.StartDate,
});
In words: given values for memberId, startDate, endDate. From the table of Claims, keep only those Claims that have a value for property MemberId that equals memberId, a value for Property startDate that is higher than startDate, and a value for endDate that is smaller than endDate.
From every Claim in the sequence of remaining Claims, make one new object with three properties.
Date is the EndDate of the Claim,
Duration is the EndDate - StartDate of the claim
If the VisityType of the Claim is in allowedTypes, then the value for property VisitType is "foo", else VisitType has a value of "bar"

Related

Filtering nullable items in the List - LINQ

I have the following List item in order to display. I could visualize the small list follows, that could be hundreds rows. StartDate and also EndDate can be nullable, if EndDate is null, it means the course still open.
CourseId ClassName StartDate EndDate isActiveinDB
-------- --------- --------- ------- ------------
12321 Math 08-25-2017 12-02-2017 Y
32342 Math 08-25-2017 12-02-2017 N
25325 Math 01-25-2018 - Y
If I pass today date (06-06-2018) in the following method, it returns me all courses rather than only the last course (Math 25325) which has not expired and open based on isActiveinDB.
I wonder what is not correct with the following implementation.
public List<Courses> GetClassesByDate( DateTime date, List<Courses> allCourses)
{
List<Courses> courses = allCourses.Where( x => x.StartDate.HasValue ? x.StartDate <= date : true
&& x.EndDate.HasValue ? x.EndDate.Value >= date : true
&& x.isActiveinDB.Equals("Y")).ToList();
return courses;
}
Thanks to #DavidG, implementation is in the following link
Try (if StartDate is nullable, as you said):
List<Courses> courses = allCourses.Where( x =>
(x.StartDate.HasValue ? x.StartDate.Value <= date : true)
&& (x.EndDate.HasValue ? x.EndDate.Value >= date : true)
&& x.isActiveinDB.Equals("Y")).ToList();
You see the () I've added? I thing what you are doing is actually
x.EndDate.HasValue ? x.EndDate.Value : (true && isActiveinDB.Equals("Y"))
You see? The true isn't a single value, but is a subexpression true && isActiveinDB.Equals("Y")
Operators are in an order to be evaluated in a expression. It is explained in https://msdn.microsoft.com/en-us/library/2bxt6kc4.aspx. There's a table that shows operators in the order in a table.
As you can see in the list, conditional expression (?:) is quite low priority. So, I recommend to put parenthesis any time around the conditional expression, then you can avoid such accident as you have.
The following code is NUnit test code for verifying the operators order. Hope Record class is close to your case.
Executing this test, the first assertion passes but the second one fails. It proves the parenthesis makes difference in such expression.
[TestFixture]
public class SyntaxTest
{
public class Record
{
public string Id;
public DateTime? StartDate;
public DateTime? EndDate;
public string isActiveinDB;
}
[TestCase]
public void TestConditionalSyntax()
{
var list = new List<Record>
{
new Record { Id = "0000", StartDate = DateTime.Parse("2018-01-01"), EndDate = DateTime.Parse("2018-06-01"), isActiveinDB = "Y" },
new Record { Id = "0001", StartDate = DateTime.Parse("2018-01-01"), EndDate = DateTime.Parse("2018-09-01"), isActiveinDB = "N" },
new Record { Id = "0002", StartDate = DateTime.Parse("2018-01-01"), EndDate = DateTime.Parse("2018-08-01"), isActiveinDB = "Y" },
new Record { Id = "0003", StartDate = DateTime.Parse("2018-01-01"), EndDate = null, isActiveinDB = "Y" },
new Record { Id = "0004", StartDate = DateTime.Parse("2018-08-01"), EndDate = null, isActiveinDB = "Y" },
new Record { Id = "0005", StartDate = null, EndDate = DateTime.Parse("2018-06-01"), isActiveinDB = "Y" },
new Record { Id = "0006", StartDate = null, EndDate = DateTime.Parse("2018-08-01"), isActiveinDB = "Y" },
};
var date = DateTime.Parse("2018-06-15");
var result1 = list.Where(x => ( x.StartDate.HasValue ? x.StartDate <= date : true )
&& ( x.EndDate.HasValue ? x.EndDate >= date : true )
&& x.isActiveinDB.Equals("Y")).ToList();
Assert.That(result1.Count, Is.EqualTo(3));
var result2 = list.Where(x => x.StartDate.HasValue ? x.StartDate <= date : true
&& x.EndDate.HasValue ? x.EndDate >= date : true
&& x.isActiveinDB.Equals("Y")).ToList();
Assert.That(result2.Count, Is.EqualTo(3));
}
}

LINQ throwing exception when attempting to compare dates

I have the following:
return db.Events
.Where(e =>
e.BedId == bed
&& e.Date.Year == date.Year
&& e.Date.Month == date.Month
&& e.Date.Day == date.Day)
.Select(x => new EventViewModel() {
Id = x.Id,
Date = x.Date.Date.ToString(),
StartTime = x.StartTime.ToString(),
EndTime = x.EndTime.ToString(),
Planned = x.Planned,
EngineSN = x.EngineSN,
Details = x.Details,
Bed = x.Bed.Name,
Category = x.Subcategory.Category.Name,
Subcategory = x.Subcategory.Name,
Project = x.Project.Name,
Type = x.Type.ToString()
})
.ToList();
It complains that member 'date' is not supported by LINQ
I am aware that LINQ can't work with dates as it has no concept of how to read and compare them. That is why I specifically asked it to compare the Year Month and Day properties (all are ints). What in my query is throwing this exception?
P.S. EventViewModel.Date is a String
You need to change your query so you aren't calling the Date property on DateTime and change your view model.
Define your DateTime properties that you defined as string as DateTime.
Annotate the DateTime properties like this
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? Date{ get; set; }
or whatever date format you are using in your UI.
Change your query like this
var startDate = date.Date;
var endDate = date.Date.AddDays(1);
return db.Events
.Where(e =>
e.BedId == bed
&& e.Date >= startDate
&& e.Date < endDate)
.Select(x => new EventViewModel()
{
Id = x.Id,
Date = x.Date,
StartTime = x.StartTime,
EndTime = x.EndTime,
Planned = x.Planned,
EngineSN = x.EngineSN,
Details = x.Details,
Bed = x.Bed.Name,
Category = x.Subcategory.Category.Name,
Subcategory = x.Subcategory.Name,
Project = x.Project.Name,
Type = x.Type.ToString()
})
.ToList();
It looks like what you're trying to do is exclude the time portion of the date in your comparison and in your projection. If that's the case, use DbFunctions.
.Where(DbFunctions.TruncateTime(e.Date == date))
and then
Date = DbFunctions.TruncateTime(x.Date)
LINQ works with Dates as mentioned by others. It is your SELECT that is complaining at x.Date.Date.ToString()

Convert OrderBy Case statement in LINQ

I'm trying to convert the following SQL from Oracle into a Linq to Entity query.
ORDER by
case when(e.prev_co = 'ABC' and(nvl(co_seniority, '1-jan-2099') < to_date('10-apr-2001')))
then '2001-04-01'
else to_char(nvl(co_seniority, '1-jan-2099'), 'YYYY-MM-DD') end,
nvl(co_seniority, '1-jan-2099'),
birth_dt
I was hoping I could use a function to pass in some parameters and have it return the correct date. I tried creating an new property called SortDate and then created a function on my page that would take in the parameters and return the correct date but that didn't work. I get and exception that says "LINQ to Entities does not recognize the method GetSortDate".
Model
SortByDate = GetSortDate(e.PREV_CO, e.CO_SENIORITY),
Function
public static DateTime GetSortDate(string PreviousCo, DateTime? CoSeniorityDate)
{
//set variable to default date
DateTime sortDate = System.DateTime.Parse("2001-04-01");
//set default date if NULL
if (CoSeniorityDate == null)
{
CoSeniorityDate = System.DateTime.Parse("2099-01-01");
}
if (PreviousCo == "ABC" && (CoSeniorityDate < System.DateTime.Parse("2001-04-10")))
{
sortDate = System.DateTime.Parse("2001-04-01");
}
else
{
sortDate = System.DateTime.Parse(CoSeniorityDate.ToString());
}
return sortDate;
}
Here is my complete EF
using (DataContext db = new DataContext())
{
db.Configuration.AutoDetectChangesEnabled = false; //no changes needed so turn off for performance.
var workStatus = new string[] { "1", "3" };
var company = new string[] { "EX", "SM" };
var eventReason = new string[] { "21", "22", "23" };
data = (from e in db.EMPLOYEE
where workStatus.Contains(e.WORKSTAT)
&& company.Contains(e.CO.Substring(0, 2))
&& ((e.EVENT_TYP != "35") || (e.EVENT_TYP == "35" && !eventReason.Contains(e.EVENT_RSN)))
select new Employee
{
Co = e.CO,
CityCode = e.CITY_CODE,
EmployeeNumber = e.EMP,
LastName = e.LAST_NAME,
FirstName = e.FIRST_NAME,
Position = e.ABV_POSITION_TITLE,
EmploymentType = e.PART_TIME_IND == "X" ? "PT" : "FT",
SeniorityDate = e.CO_SENIORITY == null ? DateTime.MaxValue : e.CO_SENIORITY,
BirthDate = e.BIRTH_DT,
SortByDate = GetSortDate(e.PREV_CO, e.CO_SENIORITY),
PreviousCo = e.PREV_CO
}).OrderBy(o => o.SortByDate).ThenBy(o => o.SeniorityDate).ThenBy(o => o.BirthDate).ToList();
}
Anyone have a suggestion on how I can convert this OrderBy?
UPDATED QUESTION
At the moment I have my query working correctly by using a secondary SELECT like #Markus showed. The first query just pulls the data and then all the formatting and calling of a method to get the correct SortByDate.
However, my manager would really prefer to do the sorting in the DB versus in memory. He let this one go because there are very few people calling this seniority list and only once a month.
For learning purposes I would like to see if I could get the DB to do all the sorting as #IvanStoev shows below. So, going back that route I’m not able to get the OrderBy to work exactly like it should.
If you look at the original SQL I’m trying to convert it first looks to see if the person had a previous company of “ABC” and if they do then look at the SeniorityDate (set a default date if that’s NULL) and compare it to an acquisition date. If that condition isn’t met then just use their SeniorityDate (set it’s default if NULL). Tricky….I know.
Using the suggested OrderBy in LinqPad and then looking at the returned SQL I can see that the first part of the OrderBy looks at the previous company and then the SeniorityDate and sets a value. Then it looks at the acquisition date. I need to somehow group some conditions to look at first which I don’t know it that’s possible.
SELECT t0.ABV_POSITION_TITLE, t0.BIRTH_DT, t0.CITY_CODE, t0.CO, t0.CO_SENIORITY, t0.EMP, t0.FIRST_NAME, t0.LAST_NAME, t0.PART_TIME_IND, t0.PREV_CO, t0.WORKSTAT
FROM SAP_EMPLOYEE t0
WHERE ((((t0.WORKSTAT IN (:p0, :p1) AND (t0.PERS_SUB_AREA = :p2)) AND SUBSTR(t0.CO, 0 + 1, 2) IN (:p3, :p4)) AND (t0.CO <> :p5)) AND ((t0.EVENT_TYP <> :p6) OR ((t0.EVENT_TYP = :p6) AND NOT t0.EVENT_RSN IN (:p7, :p8, :p9))))
ORDER BY (CASE WHEN ((t0.PREV_CO = :p10) AND (t0.CO_SENIORITY IS NULL)) THEN :p11 WHEN (t0.CO_SENIORITY < :p12) THEN :p13 ELSE COALESCE(t0.CO_SENIORITY, :p11) END), COALESCE(t0.CO_SENIORITY, :p11), t0.BIRTH_DT
-- p0 = [1]
-- p1 = [3]
-- p2 = [200A]
-- p3 = [EX]
-- p4 = [SM]
-- p5 = [EXGS]
-- p6 = [35]
-- p7 = [21]
-- p8 = [22]
-- p9 = [23]
-- p10 = [ABC]
-- p11 = [1/1/2099 12:00:00 AM]
-- p12 = [4/10/2001 12:00:00 AM]
-- p13 = [4/1/2001 12:00:00 AM]
I need to come up with something like
ORDER BY (CASE WHEN ((t0.PREV_CO = :p10) AND (COALESCE(t0.CO_SENIORITY, :p11) < :p12) THEN :p13 ELSE COALESCE(t0.CO_SENIORITY, :p11) END)
Here is the code I used in LinqPad.
void Main()
{
var workStatus = new string[] { "1", "3" };
var company = new string[] { "EX", "SM" };
var eventReason = new string[] { "21", "22", "23" };
var baseDate = new DateTime(2001, 4, 10); // 10-apr-2001
var minDate = new DateTime(2001, 4, 1); // 1-apr-2001
var abcDate = new DateTime(2001, 4, 10); // 10-apr-2001
var maxDate = new DateTime(2099, 1, 1); // 1-jan-2099
var data = (from e in SAP_EMPLOYEE
where workStatus.Contains(e.WORKSTAT)
&& e.PERS_SUB_AREA == "200A"
&& company.Contains(e.CO.Substring(0, 2))
&& e.CO != "EXGS"
&& ((e.EVENT_TYP != "35") || (e.EVENT_TYP == "35" && !eventReason.Contains(e.EVENT_RSN)))
orderby e.PREV_CO == "ABC" && e.CO_SENIORITY == null ? maxDate : e.CO_SENIORITY < abcDate ? minDate : e.CO_SENIORITY ?? maxDate,
e.CO_SENIORITY ?? maxDate,
e.BIRTH_DT
select new Employee
{
Co = e.CO,
CityCode = e.CITY_CODE,
EmployeeNumber = e.EMP,
LastName = e.LAST_NAME,
FirstName = e.FIRST_NAME,
Position = e.ABV_POSITION_TITLE,
EmploymentType = e.PART_TIME_IND == "X" ? "PT" : "FT",
SeniorityDate = e.CO_SENIORITY == null ? maxDate :
e.PREV_CO == "ABC" && e.CO_SENIORITY < twaDate ? maxDate : e.CO_SENIORITY,
LOA = e.WORKSTAT == "1" ? "LOA" : "",
ABC = e.PREV_CO == "ABC" ? "ABC" : "",
BirthDate = e.BIRTH_DT,
PreviousCo = e.PREV_CO
}).ToList();
data.Dump();
}
The reason for the exception is that entity framework generates the SQL query when you execute it. In your case, this happens with the call to ToList() at the end. In order to generate the SQL query, entity framework analyzes the query and transforms it into SQL. As entity framework does not know your function, it cannot generate the SQL statements for it.
In order to solve this, you need to first execute the query and do the sort operation in memory on the results. In order to limit the amount of data that is transferred to the client, you should execute the query including the where clause and also tell EF which fields you are interested in to avoid a SELECT * FROM ... that includes all fields of the table.
You could change your query approximately as follows:
data = (from e in db.EMPLOYEE
where workStatus.Contains(e.WORKSTAT)
&& company.Contains(e.CO.Substring(0, 2))
&& ((e.EVENT_TYP != "35") || (e.EVENT_TYP == "35" && !eventReason.Contains(e.EVENT_RSN)))
select new ()
{
Co = e.CO,
CityCode = e.CITY_CODE,
EmployeeNumber = e.EMP,
LastName = e.LAST_NAME,
FirstName = e.FIRST_NAME,
Position = e.ABV_POSITION_TITLE,
EmploymentType = e.PART_TIME_IND == "X" ? "PT" : "FT",
SeniorityDate = e.CO_SENIORITY,
BirthDate = e.BIRTH_DT,
PreviousCo = e.PREV_CO
}).ToList().Select(x => new Employee()
{
Co = x.Co,
CityCode = x.CityCode,
EmployeeNumber = x.EmployeeNumber,
LastName = x.LastName,
FirstName = x.FirstName,
Position = x.Position,
EmploymentType = x.EmploymentType,
SeniorityDate = x.SeniorityDate ?? DateTime.MaxValue,
BirthDate = x.BirthDate,
SortByDate = GetSortDate(x.PreviousCo, x.SeniorityDate),
PreviousCo = x.PreviousCo
}).OrderBy(o => o.SortByDate)
.ThenBy(o => o.SeniorityDate)
.ThenBy(o => o.BirthDate).ToList();
This query first filters the data as specified in the where clause and then uses an anonymous type to retrieve only the relevant fields - including the ones that are later used as an input to the GetSortDate method with its original values. After the first ToList the results are present in memory and you can first add a new select that creates the Employee objects including the sort date. These objects are then ordered by sort date and so on.
A small hint for the GetSortDate method: specifying DateTime constants as a string that is parsed is not a good idea as parsing is dependent on the culture of the thread (if no culture is specified).
// Culture dependent
sortDate = System.DateTime.Parse("2001-04-01");
// Better
sortDate = new DateTime(2001, 04, 01);
As you already noticed (the hard way), in LINQ to Entities query you cannot use local methods like in LINQ to Objects. If you want the whole query to be executed in the database, you need to embed the logic inside the query using only the supported constructs.
With that being said, the equivalent of your SQL query should be something like this
var baseDate = new DateTime(2001, 4, 10); // 10-apr-2001
var minDate = new DateTime(2001, 4, 1); // 1-apr-2001
var maxDate = new DateTime(2099, 1, 1); // 1-jan-2099
data = (from e in db.EMPLOYEE
where workStatus.Contains(e.WORKSTAT)
&& company.Contains(e.CO.Substring(0, 2))
&& ((e.EVENT_TYP != "35") || (e.EVENT_TYP == "35" && !eventReason.Contains(e.EVENT_RSN)))
let seniorityDate = e.CO_SENIORITY ?? maxDate
let sortDate =
e.CO_SENIORITY == null ? maxDate :
e.PREV_CO == "ABC" && e.CO_SENIORITY < baseDate ? minDate :
e.CO_SENIORITY
orderby sortDate, seniorityDate, e.BIRTH_DT
select new Employee
{
Co = e.CO,
CityCode = e.CITY_CODE,
EmployeeNumber = e.EMP,
LastName = e.LAST_NAME,
FirstName = e.FIRST_NAME,
Position = e.ABV_POSITION_TITLE,
EmploymentType = e.PART_TIME_IND == "X" ? "PT" : "FT",
SeniorityDate = e.CO_SENIORITY,
BirthDate = e.BIRTH_DT,
PreviousCo = e.PREV_CO
}).ToList();
Update: For learning purposes I've updated the answer with using let clauses.
Now regarding the concrete ordering. I could have written the "SortDate" part exactly the way you did it, but I believe my way is a better equivalent. Why?
Here is my "SortDate" interpretation in pseudo code
if (CoSeniorityDate == null)
SortDate = #2099-01-01#
else if (PreviousCo == "ABC" && CoSeniorityDate < #2001-04-10#)
SortDate = #2001-04-01#
else
SortDate = CoSeniorityDate
And here is your function
if (CoSeniorityDate == null) CoSeniorityDate = #2099-01-01#
if (PreviousCo == "ABC" && CoSeniorityDate < #2001-04-10#)
SortDate = #2001-04-01#
else
SortDate = CoSeniorityDate
Let CoSeniorityDate == null. Then, according to your logic, let substitute CoSeniorityDate = #2099-01-01#:
if (PreviousCo == "ABC" && #2099-01-01# < #2001-04-10#)
SortDate = #2001-04-01#
else
SortDate = #2099-01-01#
Since #2099-01-01# < #2001-04-10# is always false, it becomes simple
SortDate = #2099-01-01#
i.e. exactly like the first part of my criteria. In the else part we already know CoSeniorityDate is not null and can just check the other conditions.
Anyway, doing it your way would be like this
let sortDate = e.PREV_CO == "ABC" && seniorityDate < baseDate ? minDate : seniorityDate

How to group linq expression only by date in datetime field

Hello I need to run the following
query and group datetime field only by the date value.
var startDateTime = regDate.Date;
var endDateTime = regDate.AddSeconds(fullDayinSeconds);
var res = from u in ObjectContext.Member.Where(o => o.RegisterTime >= startDateTime && o.RegisterTime <= endDateTime)
group u by new { u.PartnerId, u.RegisterTime.Date, u.partners.Name } into pgroup
let count = pgroup.Count()
select new PartnersUsersInfo
{
PartnerId = pgroup.Key.PartnerId.GetValueOrDefault(0),
PartnerName = pgroup.Key.Name, PartnerUsersAmount = count
};
u.RegisterTime.Date - returns the exception The specified type member 'Date' is not supported in LINQ to Entities.
I have tried to use EntityFunctions.TruncateTime but it is not accepteble for group operations.
how to solve it ?
you can try add temp col with date and group by it
use
db.CallLogs.Where(r => DbFunctions.TruncateTime(r.DateTime) == callDateTime.Date).ToList();

LINQ to SQL Conditional where clause

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:
if SiteTypeId = -1 (then this means show all so nothing is to be changed)
if SiteTypeId = 0 (then i.SiteTypeId == 0 needs to be added)
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);
}
Sounds like you could use LINQKit and its PredicateBuilder. You use it to build dynamic conditional WHERE clauses. It's also used in LinqPad, and it's free.
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);
}
Simply add the following to your where clause
(SiteTypeId == -1 || i.SiteTypeId == SiteTypeId)

Categories

Resources