Compound Where Clause in LINQ not working correctly - linq

I have a screen where the user can enter in different values to add to the WHERE clause. When I test with just 1 value my code works fine. When I test with more than 1 value, the result sets are always 0.
I have validated the data in the database and the data coming from the form are as expected. Them multiple items in the WHERE predicate are not matching correctly.
whereClause is of type Dictionary
var lazydata = (from item in db.ComplaintLogWithRetailer
select item);
string val = "";
if (whereClause != null)
{
if (whereClause.Keys.Contains("CaseYear"))
{
val = whereClause["CaseYear"];
lazydata = lazydata.Where(w => w.CaseYearOnly.ToString().Equals(val));
}
if (whereClause.Keys.Contains("AssignedTo"))
{
val = whereClause["AssignedTo"];
lazydata = lazydata.Where(w => w.Initials.ToString().Equals(val));
}
if (whereClause.Keys.Contains("CaseNumber"))
{
val = whereClause["CaseNumber"];
lazydata = lazydata.Where(w => w.CaseNumber.ToString().Equals(val));
}
//if (whereClause.Keys.Contains("CountyId"))
// data = lazydata.Where(w => w.Count.ToString().Equals(whereClause["CountyId"])).ToList();
if (whereClause.Keys.Contains("ClassCode"))
lazydata = lazydata.Where(w => w.ClassCode.ToString().Equals(whereClause["ClassCode"]));
if (whereClause.Keys.Contains("DateReceived"))
lazydata.Where(w => w.ReceivedDate.ToString().Equals(whereClause["DateReceived"]));
if (whereClause.Keys.Contains("RetailerNumber"))
lazydata = lazydata.Where(w => w.RetailerNumber.ToString().Equals(whereClause["RetailerNumber"]));
data = lazydata.ToList();
//data = lazydata.ToList<ComplaintLogWithRetailer>();
//data = (from item in lazydata
// select item).ToList();
}
There are 5 records with a year of 2018 and 1 of those records has AssignedTo of 'BCC' I am expecting 1 records back but always get 0 records when 2 or more '.Where" operations are performed

Related

LinqKit Core PredicateBuilder not functioning correctly

I have this query:
var query = LinqKit.PredicateBuilder.New<Resume>();
if (selectedWorkFieldID != 0)
{
query = query.And(js => js.WorkFieldID == selectedWorkFieldID);
if (!(selectedJobIDs.Contains(0) && selectedJobIDs.Count() == 1))
{
foreach (int jobID in selectedJobIDs)
query = query.Or(js => js.JobID == jobID);
}
}
var finalQuery = context.Resumes.AsNoTracking().Include(r => r.ResumeSkills)
.ThenInclude(rs => rs.Skill).Include(r => r.JobSeeker).ThenInclude(r => r.Profile)
.AsExpandable().Where(query);
count = finalQuery.Count();
resumes = finalQuery.Skip(args.Skip.Value).Take(args.Top.Value).ToList<Resume>();
This query returns All resumes not filtered ones. When I debug, the debugger curser enters the foreach block that filters with or, and there is one jobID in selectedJobIDs but the query returns all resumes. it seems the predicate builder not working at all. How to solve this?
I changed code to this:
if (selectedWorkFieldID != 0)
{
query = query.And(js => js.WorkFieldID == selectedWorkFieldID);
if (!(selectedJobIDs.Contains(0) && selectedJobIDs.Count() == 1))
{
var query2 = LinqKit.PredicateBuilder.New<Resume>();
foreach (int jobID in selectedJobIDs)
query2 = query2.Or(js => js.JobID == jobID);
query.And(query2);
}
}
and it is corrected.

Count nulls in properties with one LINQ query

Paste into LINQPad:
void Main()
{
List<Data> list = new List<Data>();
list.Add(new Data());
list.Add(new Data{a="a", b="b"});
list.Add(new Data{a=null, b="b"});
var queryA = from data in list where data.a == null select data;
var queryB = from data in list where data.b == null select data;
var countNulls = new {a = queryA.Count(),b = queryB.Count()};
countNulls.Dump();
}
class Data
{
public string a {get;set;}
public string b {get;set;}
}
Instead of using queryA and queryB is it possible to do this in one query?
Answer:
All queries below generate exactly same SQL, so it's just a preference of coder what to choose.
var countNulls = new
{
a = queryA.Count(),
b = queryB.Count()
};
var countNulls2 = new
{
a = list.Count(d => d.a == null),
b = list.Count(d => d.b == null)
};
var countNulls3 = list.Aggregate(
new { a = 0, b = 0 },
(acc, data) => new
{
a = acc.a + (data.a == null ? 1 : 0),
b = acc.b + (data.b == null ? 1 : 0),
});
Update: apparently (thanks to Evan Stoev) this task can be done 20x faster on EF's DbSet and it creates one SQL query.
var countNulls4 =
(from data in db.Data
group data by 1 into g
select new
{
a = g.Sum(data => data.a == null ? 1 : 0),
b = g.Sum(data => data.b == null ? 1 : 0)
}).First();
For completeness, you can use Aggregate to get the result in a single pass over the input sequence:
var countNulls = list.Aggregate(
new { a = 0, b = 0 },
(acc, data) => new
{
a = acc.a + (data.a == null ? 1 : 0),
b = acc.b + (data.b == null ? 1 : 0),
});
But I'm not sure it would be more efficient compared to 2 separate Count calls due to the need of anonymous object allocation on each step.
UPDATE: It turns out that you are asking for a single SQL query (so list is not actually a List<Data> but DbSet<Data> I guess). In LINQ to Entities, you can use group by constant technique, which combined with replacing Count(condition) with Sum(condition ? 1 : 0) will produce a nice single SQL query pretty similar to what you would write manually:
var countNulls =
(from data in db.Data
group data by 1 into g
select new
{
a = g.Sum(data => data.a == null ? 1 : 0),
b = g.Sum(data => data.b == null ? 1 : 0)
}).First();
You can simply use the Count() overload that takes a predicate:
var countNulls = new
{
a = list.Count(d => d.a == null),
b = list.Count(d => d.b == null)
};
Output is:
As far as I understand, you want to count nulls for two different object properties regardless if the other one is null. In other words:
a=null, b="b"
a="a", b=null
a=null, b=null
would return count for 2 and 2.
In that case you can do it this way in one query:
var queryAB = from data in list where data.a == null || data.b == null select data;

LINQ in Business Layer paging / sorting gridview in User layer

I have a gridview in my user layer that uses a business layer method as its datasource, and I want the gridview to support paging and sorting. When I returned an Ienumerable from the method, it will bring back all of the data. If I use Take/Skip to bring back only a page's worth, the gridview doesn't realize that there are many pages of data.
When I change the Ienumerable to an IQueryable, the DataBind() fails because the data has already been disposed of. I think this problem has something to do with when the query actually executes and it may have to do with the filters I am applying to the query (see below).
User Layer Code:
grdSelectedQuestionaires.DataSource = assessment.FilteredAssessmentList(filter);
grdSelectedQuestionaires.DataBind(); <-- Fails - "Cannot access a disposed object"
Business Layer Code:
public IQueryable<Assessment> FilteredAssessmentList(AssessmentSearchFilter filter)
{
using (PAQcDataLayerDataContext dc = new PAQcDataLayerDataContext())
{
var rows = (
from a in dc.vPAQSummaries
select new Assessment()
{
PAQNumber = a.PAQNumber.Trim(),
CustomerID = (int)a.CustomerID,
Department = a.Department.Trim(),
CustomerName = a.CustomerName.Trim(),
DOTNumber = a.DOTNumber.Trim(),
OrgName = a.OrgName.Trim(),
DateEntered = a.DateEntered,
GroupNumber = a.GroupNumber.Trim(),
JobTitle = a.JobTitle.Trim(),
FileNames = a.FileNames.Trim(),
AnalystType = a.AnalystType.ToString(),
Incumbents = a.Incumbents,
});
// Filter by Customer ID
if (filter.CustomerID > 0)
{
rows = rows.Where(r => r.CustomerID == filter.CustomerID);
}
else
{
rows = rows.Where(r => r.CustomerID != null);
}
// Filter by DOT Number
if (!string.IsNullOrEmpty(filter.DOTNumberFrom))
{
if (string.IsNullOrEmpty(filter.DOTNumberTo))
{
rows = rows.Where(r => r.DOTNumber == filter.DOTNumberFrom);
}
else
{
rows = rows.Where(r => r.DOTNumber.CompareTo(filter.DOTNumberFrom) >= 0
&& r.DOTNumber.CompareTo(filter.DOTNumberTo) <= 0);
}
}
// Filter by OrgName
if (!string.IsNullOrEmpty(filter.OrgName))
{
rows = rows.Where(r => r.OrgName.StartsWith(filter.OrgName));
}
// Filter by Group
if (!string.IsNullOrEmpty(filter.GroupNumberFrom))
{
if (!string.IsNullOrEmpty(filter.GroupNumberTo))
{
rows = rows.Where(r => r.GroupNumber == filter.GroupNumberFrom);
}
else
{
rows = rows.Where(r => r.GroupNumber.CompareTo(filter.GroupNumberFrom) >= 0
&& r.GroupNumber.CompareTo(filter.GroupNumberTo) <= 0);
}
}
if (filter.Skip > 0)
{
rows = rows.Skip(filter.Skip);
}
if (filter.Take > 0)
{
rows = rows.Take(filter.Take);
}
else
{
rows = rows.Take(100);
}
return rows.Distinct();
}
}
}
The paging problem was caused by the Using statement. Changing this:
using (PAQcDataLayerDataContext dc = new PAQcDataLayerDataContext())
{
...
}
to this:
PAQcDataLayerDataContext dc = new PAQcDataLayerDataContext();
Made paging work. Now I have to figure out how sorting works.

LINQ: Group By + Where in clause

I'm trying to implement a T-SQL equivalent of a where in (select ...) code in LINQ.
This is what I have now:
int contactID = GetContactID();
IEnumerable<string> threadList = (from s in pdc.Messages
where s.ContactID == contactID
group 1 by new { s.ThreadID } into d
select new { ThreadID = d.Key.ThreadID}).ToList<string>();
var result = from s in pdc.Messages
where threadList.Contains(s.ThreadID)
group new { s } by new { s.ThreadID } into d
let maxMsgID = d.Where(x => x.s.ContactID != contactID).Max(x => x.s.MessageID)
select new {
LastMessage = d.Where(x => x.s.MessageID == maxMsgID).SingleOrDefault().s
};
However, my code won't compile due to this error for the ToList():
cannot convert from
'System.Linq.IQueryable<AnonymousType#1>'
to
'System.Collections.Generic.IEnumerable<string>'
Anyone have any suggestions on how to implement this? Or any suggestions on how to simplify this code?
Your query returns a set of anonymous types; you cannot implicitly convert it to a List<string>.
Instead, you should select the string itself. You don't need any anonymous types.
Change it to
var threadList = pdc.Messages.Where(s => s.ContactID == contactID)
.Select(s => s.ThreadID)
.Distinct()
.ToList();
var result = from s in pdc.Messages
where threadList.Contains(s.ThreadID)
group s by s.ThreadID into d
let maxMsgID = d.Where(x => x.ContactID != contactID).Max(x => x.MessageID)
select new {
LastMessage = d.Where(x => x.MessageID == maxMsgID).SingleOrDefault()
};

LinQ query - Add Where dynamically

I am having a hard time solving this problem, need code for creating a dynamic linq query in C#, asp.net. I have 5 dropdown list that searches different column in same database table and return item filtered value to a single listbox. The problem is there is no sequence that which or all or any will be selected in DDLs but the combined filtered result should show up in listbox. I have a working query that is searching and returning result in one column at a time for each DDL selection separately. Have to add where clauses with AND to add other DDL selections dynamically to this query. Thanks
public ListItemCollection searchProject(ListItemCollection projList, String searchstr, String columnName)
{
DataSet DSToReturn = new DataSet();
ListItemCollection returnItems = new ListItemCollection();
DataTable results = (from d in ((DataSet)_MyDataset).Tables["Records"].AsEnumerable()
orderby d.Field<string>("Name") ascending
where (d.Field<string>(columnName) != null)
where d[columnName].ToString().ToLower().Contains(searchstr.ToLower())
select d).CopyToDataTable();
foreach (ListItem li in projList)
{
if ((from System.Data.DataRow row in results.Rows
where li.Value.Equals(row["value"].ToString(), StringComparison.InvariantCultureIgnoreCase)
select row["value"]).Count() > 0)
returnItems.Add(li);
}
return returnItems;
}
Here's some example code for how we do it ...
private void DataPortal_Fetch(GoalCriteria criteria)
{
using (var ctx = ContextManager<Data.ExodusDataContext>
.GetManager(Database.ApplicationConnection, false))
{
this.RaiseListChangedEvents = false;
this.IsReadOnly = false;
// set option to eager load child object(s)
var opts = new System.Data.Linq.DataLoadOptions();
opts.LoadWith<Data.Goal>(row => row.Contact);
opts.LoadWith<Data.Goal>(row => row.Sales);
opts.LoadWith<Data.Goal>(row => row.Customer);
ctx.DataContext.LoadOptions = opts;
IQueryable<Data.Goal> query = ctx.DataContext.Goals;
if (criteria.Name != null) // Name
query = query.Where(row => row.Name.Contains(criteria.Name));
if (criteria.SalesId != null) // SalesId
query = query.Where(row => row.SalesId == criteria.SalesId);
if (criteria.Status != null) // Status
query = query.Where(row => row.Status == (int)criteria.Status);
if (criteria.Statuses.Count != 0) // Statuses
query = query.Where(row => criteria.Statuses.Contains((GoalStatus)row.Status));
if (criteria.ContactId != null) // ContactId
query = query.Where(row => row.ContactId == criteria.ContactId);
if (criteria.CustomerId != null) // CustomerId
query = query.Where(row => row.CustomerId == criteria.CustomerId);
if (criteria.ScheduledDate.DateFrom != DateTime.MinValue) // ScheduledDate
query = query.Where(t => t.ScheduledDate >= criteria.ScheduledDate.DateFrom);
if (criteria.ScheduledDate.DateTo != DateTime.MaxValue)
query = query.Where(t => t.ScheduledDate <= criteria.ScheduledDate.DateTo);
if (criteria.CompletedDate.DateFrom != DateTime.MinValue) // ComplatedDate
query = query.Where(t => t.CompletedDate >= criteria.CompletedDate.DateFrom);
if (criteria.CompletedDate.DateTo != DateTime.MaxValue)
query = query.Where(t => t.CompletedDate <= criteria.CompletedDate.DateTo);
if (criteria.MaximumRecords != null) // MaximumRecords
query = query.Take(criteria.MaximumRecords.Value);
var data = query.Select(row => GoalInfo.FetchGoalInfo(row));
this.AddRange(data);
this.IsReadOnly = true;
this.RaiseListChangedEvents = true;
}
}
We just check for a null value assigned to our criteria object, if it's not null then we append it to the query.

Resources