Datetime check in Linq where statement - c#

Please see the code below:
var pcPageList = db.PcPages
.Where(m =>
m.Quarter == exactQuarter &&
m.Url == pageUrl &&
m.UpdatedOn.ToDateTime().Date.ToString("dd/MMM").ToLower() == "02/nov")
.OrderBy(m => m.UpdatedOn)
.FirstOrDefault();
When I run this above, the application throws error says: "ToDateTime" is not implemented yet. Anyone please can advice ?

How about:
var updateStart = DateTime.ParseExact("02/nov", "dd/MMM", CultureInfo.InvariantCulture);
var updateEnd = updateStart.AddDays(1.0);
var pcPageList = db.PcPages
.Where(m =>
m.Quarter == exactQuarter &&
m.Url == pageUrl &&
m.UpdatedOn >= updateStart &&
m.UpdatedOn < updateEnd)
.OrderBy(m => m.UpdatedOn)
.FirstOrDefault();

I think, you should be calling ToDateTime using Convert class as:
Convert.ToDateTime(m.UpdatedOn).Date...
And remove the Date in between as:
Convert.ToDateTime(m.UpdatedOn).ToString("dd/MMM").ToLower() == "02/nov"

Rather than doing a string comparison, it would be more efficient to compare the date components directly. I haven't tested this, but something like the following may work:
var pcPageList = db.PcPages
.Where(m => m.Quarter == exactQuarter && m.Url == pageUrl)
// You may need to materialize the results of the query at this point
// or use Convert.ToDateTime(...) instead of ToDateTime()
.Select(m => new { Row = m, UpdatedOn = m.UpdatedOn.ToDateTime() })
.Where(a => a.UpdatedOn.Month == 11 && a.UpdatedOn.Day == 2)
.Select(a => a.Row)
.OrderBy(m => m.UpdatedOn)
.FirstOrDefault();

Related

Is there a way to set this entity/linq query to IEnumerable?

I'm trying to return an IEnumerable activities instead of "var"
var activities = ctx.Activities.Where(a => a.SiteID == propID)
.Where(a => a.ActivityTypeName == "Call")
.Select(x => new
{
x.DateTimeEntry,
x.Contact.OwnerContact.ParcelDatas
.FirstOrDefault(a => a.OwnerContactID == x.Contact.OwnerContact.OOwnerID)
.Parcel_LetterTracking.LMailDate,
x.FAQs.FirstOrDefault(a => a.ActivityID == x.ActivityID)
.FAQ_Library.FaqNum,
x.FAQs.FirstOrDefault(a => a.ActivityID == x.ActivityID)
.FAQ_Library.Question
});
edit: data type Object compiles but I'm not sure if that's right.
.Select already returns a an IEnumerable<TResult> also combine your ..where() clauses with && instead. https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select?view=netframework-4.8 also one other thing you can do is use .AsEnuemerable()
var activities = ctx.Activities.Where(a => a.SiteID == propID && a.ActivityTypeName == "Call")
.Select(x => new
{
x.DateTimeEntry,
x.Contact.OwnerContact.ParcelDatas.FirstOrDefault(a => a.OwnerContactID == x.Contact.OwnerContact.OOwnerID).Parcel_LetterTracking.LMailDate,
x.FAQs.FirstOrDefault(a => a.ActivityID == x.ActivityID).FAQ_Library.FaqNum,
x.FAQs.FirstOrDefault(a => a.ActivityID == x.ActivityID).FAQ_Library.Question
}).AsEnumerable();

Check property for null LINQ

I have the following code that give me data from database:
var t = (from ula in proxy.eUserLoginAttempts
where ula.Date >= DateTime.Now && ula.Email.ToLower().Contains("")
&& ula.User != null
&& ula.User.Client != null
&& ula.User.Client.prStatus == 1
select ula).ToList();
In this case, I would get prStatus from Client entity and I check User and Client object if they are not null. Should I do it or ula.User.Client.prStatus will translate in Inner Join and this check is needless?
Answering your direct question: NO, you should test the nullable first...
About your code, I really do suggest a readable way:
var t = proxy.eUserLoginAttempts
.Where(ula => ula.Date >= DateTime.Now)
.Where(ula => !string.IsNullOrEmpty(ula.Email))
.Where(ula => ula.User != null)
.Where(ula => ula.User.Client != null)
.Where(ula => ula.User.Client.prStatus == 1)
.ToList();
Or even better with C# 6
var t = proxy.eUserLoginAttempts
.Where(ula => ula.Date >= DateTime.Now)
.Where(ula => !string.IsNullOrEmpty(ula.Email))
.Where(ula => ula.User?.Client?.prStatus == 1)
.ToList();

Filter child entity LINQ methods

Consider the following query :
IQueryable<Employee> ret = this.ObjectContext.Employees
.Include("EmployeeInformation")
.Include("LatestInformation")
.Where(emp => emp.idJobTitle == 1 && emp.idCrtLoc == 1);
The Employees entity doesn't have a navigation property to LatestInformation's entity(So I can't directly access the other entity) but the LatestInformation does have a navigation property to the Employees entity.
How can I filter the LatestInformation entity of this query?
The expected query should look like this :
ret = ret.Where(r=> LatestInformation.Where(li => li.year == 2015)); // Ofcourse this piece of code is wrong.
So , the question was how to filter the LatestInformation entity ?
If there is no navigation property from Employee to LatestInformation, query it the other way around. Something like:
var ret = this.ObjectContext.LatestInfomration
.Where(i => i.Employee != null && i.year == 2015)
.Select(i => i.Employee)
.Where(emp => emp.idJobTitle == 1 && emp.idCrtLoc == 1);
Edited to match OP comment below:
// Extract needed data:
var employeeIdListWithInfo = this.ObjectContext.LatestInfomration
.Where(i => i.Employee != null)
.Select(i => i.Employee.Id)
.ToList();
// Build the query (not executed yet)
var employeeWithInformation = this.ObjectContext.LatestInfomration
.Where(i => i.Employee != null && i.year == 2015)
.Select(i => i.Employee)
.Where(emp => emp.idJobTitle == 1 && emp.idCrtLoc == 1);
var lonelyEmployees = this.ObjectContext.Employee
.Where(emp => !employeeIdListWithInfo.Contains(emp.Id));
var ret = employeeWithInformation.Union(lonelyEmployees);
What do you mean by navigation property?
If its some unique id then you could use
List<int> latest = this.ObjectContext.LatestInformation.Select(lat=> lat.someid).ToList();
ret = ret.Where(r=> latest.Contains(ret.id)).Where(emp => emp.idJobTitle == 1 && emp.idCrtLoc == 1);

How to combine the multiple part linq into one query?

Operator should be ‘AND’ and not a ‘OR’.
I am trying to refactor the following code and i understood the following way of writing linq query may not be the correct way. Can somone advice me how to combine the following into one query.
AllCompany.Where(itm => itm != null).Distinct().ToList();
if (AllCompany.Count > 0)
{
//COMPANY NAME
if (isfldCompanyName)
{
AllCompany = AllCompany.Where(company => company["Company Name"].StartsWith(fldCompanyName)).ToList();
}
//SECTOR
if (isfldSector)
{
AllCompany = AllCompany.Where(company => fldSector.Intersect(company["Sectors"].Split('|')).Any()).ToList();
}
//LOCATION
if (isfldLocation)
{
AllCompany = AllCompany.Where(company => fldLocation.Intersect(company["Location"].Split('|')).Any()).ToList();
}
//CREATED DATE
if (isfldcreatedDate)
{
AllCompany = AllCompany.Where(company => company.Statistics.Created >= createdDate).ToList();
}
//LAST UPDATED DATE
if (isfldUpdatedDate)
{
AllCompany = AllCompany.Where(company => company.Statistics.Updated >= updatedDate).ToList();
}
//Allow Placements
if (isfldEmployerLevel)
{
fldEmployerLevel = (fldEmployerLevel == "Yes") ? "1" : "";
AllCompany = AllCompany.Where(company => company["Allow Placements"].ToString() == fldEmployerLevel).ToList();
}
Firstly, unless AllCompany is of some magic custom type, the first line gives you nothing.
Also I have a doubt that Distinctworks the way You want it to. I don't know the type of AllCompany but I would guess it gives you only reference distinction.
Either way here'w what I think You want:
fldEmployerLevel = (fldEmployerLevel == "Yes") ? "1" : "";
var result = AllCompany.Where(itm => itm != null)
.Where(company => !isfldCompanyName || company["Company Name"].StartsWith(fldCompanyName))
.Where(company => !isfldSector|| fldSector.Intersect(company["Sectors"].Split('|')).Any())
.Where(company => !isfldLocation|| fldLocation.Intersect(company["Location"].Split('|')).Any())
.Where(company => !isfldcreatedDate|| company.Statistics.Created >= createdDate)
.Where(company => !isfldUpdatedDate|| company.Statistics.Updated >= updatedDate)
.Where(company => !isfldEmployerLevel|| company["Allow Placements"].ToString() == fldEmployerLevel)
.Distinct()
.ToList();
Edit:
I moved Distinct to the end of the query to optimize the processing.
How about trying like this;
AllCompany = AllCompany .Where(company => (company => company.Statistics.Created >= createdDate)) && (company.Statistics.Updated >= updatedDate));
If every part of query is optional (like created date, last update date..) then you can build linq query string.
Here's a sneaky trick. If you define the following extension method in its own static class:
public virtual IEnumerable<T> WhereAll(params Expression<Predicate<T> filters)
{
return filters.Aggregate(dbSet, (acc, element) => acc.Where(element));
}
then you can write
var result = AllCompany.WhereAll(itm => itm != null,
company => !isfldCompanyName || company["Company Name"].StartsWith(fldCompanyName),
company => !isfldSectorn || fldSector.Intersect(company["Sectors"].Split('|')).Any(),
company => !isfldLocation || fldLocation.Intersect(company["Location"].Split('|')).Any(),
company => !isfldcreatedDate || company.Statistics.Created >= createdDate,
company => !isfldUpdatedDate || company.Statistics.Updated >= updatedDate,
company => !isfldEmployerLevel || company["Allow Placements"].ToString() == fldEmployerLevel)
.Distinct()
.ToList();

Mixing two linq statement into one?

I have 2 linq statements, both of them are fully working. I am wondering if it is possible to mix them into one and get proper list after one linq.
var result = list3.Where(Srodek => list4.Any(x => x == Srodek.Srodek.category1) &&
(Srodek.Srodek.Source.Device == _text || Srodek.Srodek.ID.Device == _text))
.ToList();
var list666 = list3.Select(obj => new { obj, dt = DateTime.ParseExact(obj.LeftColumn, dateFormat, CultureInfo.InvariantCulture) })
.Where(x => x.dt >= czas11 && x.dt <= czas22)
.Select(x => x.obj).ToList();
LINQ methods return IEnumerable<T>, and can operate on IEnumerable<T>.
You can write
sequence.Where(...).Select(...)
One list:
var result = list3.Where(obj => {
var dt = DateTime.ParseExact(obj.LeftColumn, dateFormat, CultureInfo.InvariantCulture);
return (list4.Any(x => x == obj.Srodek.category1) &&
(obj.Srodek.Source.Device == _text || obj.Srodek.ID.Device == _text)) ||
(dt >= czas11 && dt <= czas22);})
.ToList();
Why you cant just mix them? list.where(.........).select(.......).toList();
var result = list3.Where(Srodek => list4.Any(x => x == Srodek.Srodek.category1) &&
(Srodek.Srodek.Source.Device == _text || Srodek.Srodek.ID.Device== _text))
.Select(obj => new { obj, dt = DateTime.ParseExact(obj.LeftColumn, dateFormat, CultureInfo.InvariantCulture) })
.Where(x => x.dt >= czas11 && x.dt <= czas22)
.Select(x => x.obj)
.ToList();
You can join both linq queries using Union.
list3.Where(Srodek => list4.Any(x => x == Srodek.Srodek.category1) &&
(Srodek.Srodek.Source.Device == _text || Srodek.Srodek.ID.Device == _text))
.Union(list3.Select(obj => new { obj, dt = DateTime.ParseExact(obj.LeftColumn, dateFormat, CultureInfo.InvariantCulture) })
.Where(x => x.dt >= czas11 && x.dt <= czas22)
.Select(x => x.obj));

Categories