I need to do this :
context.Ads.Where(c => c.PublishedDate.HasValue &&
c.EndDate.HasValue &&
c.EndDate.Value.AddTicks(c.PublishedDate.Value.Ticks) > currentTime &&
c.AdStatusMail.Where(b => b.StatusKey != (int)AdStatusMailKey.EndedRemainder && b.StatusKey != (int)AdStatusMailKey.MiddleRemainder).Count() < 1)
.ToList();
The problem is that AddTicks can´t be used in Linq. I have looked at the EntityFunctions but can´t see how to use it to do what I need?
IQueryable can't handle the ticks, make it IEnumerable (or tolist). Here is an example:
context.Ads.Where(c => c.PublishedDate.HasValue && c.EndDate.HasValue && c.AdStatusMail.Where(b => b.StatusKey != (int)AdStatusMailKey.EndedRemainder && b.StatusKey != (int)AdStatusMailKey.MiddleRemainder).Count() < 1)
.AsEnumerable()
.Where (c => c.EndDate.Value.AddTicks(c.PublishedDate.Value.Ticks) > currentTime)
.ToList();
Related
I have some code to search for records created between two input dates (from and to) based upon the createdDate, however we have just imported some old data and not all of the records have a createdDate which means that some records are not returned. My idea is that when there is no createdDate, I use the plannedStartDate instead. This is my original code:
mainTables = mainTables.Where(d =>
d.n.CreatedDate >= AdminSearchVm.AdminSearch.SubmittedFrom &&
d.n.CreatedDate <= AdminSearchVm.AdminSearch.SubmittedTo);
but I would like to do something like this:
mainTables = mainTables.Where(d =>
d.n.CreatedDate == null ? d.n.PlannedStartDate :
d.n.CreatedDate >=
AdminSearchVm.AdminSearch.SubmittedFrom && d.n.CreatedDate == null ?
d.n.PlannedStartDate :
d.n.CreatedDate <= AdminSearchVm.AdminSearch.SubmittedTo);
try this
mainTables = mainTables.Where(d =>
((d.n.CreatedDate == null && d.n.PlannedStartDate >= AdminSearchVm.AdminSearch.SubmittedFrom)
|| (d.n.CreatedDate != null && d.n.CreatedDate >= AdminSearchVm.AdminSearch.SubmittedFrom))
&& ((d.n.CreatedDate == null && d.n.PlannedStartDate <= AdminSearchVm.AdminSearch.SubmittedIo)
|| (d.n.CreatedDate != null && d.n.CreatedDate <= AdminSearchVm.AdminSearch.SubmittedIo)));
or you can like this
mainTables = mainTables.Where(d =>
(d.n.CreatedDate == null
&& d.n.PlannedStartDate >= AdminSearchVm.AdminSearch.SubmittedFrom
&& d.n.PlannedStartDate <= AdminSearchVm.AdminSearch.SubmittedTo)
||
(d.n.CreatedDate != null
&& d.n.CreatedDate >= AdminSearchVm.AdminSearch.SubmittedFrom
&& d.n.CreatedDate <= AdminSearchVm.AdminSearch.SubmittedIo));
Maybe you can use this approach:
mainTables = mainTables
.Select(t => new { Table = t, Date = (t.n.CreatedDate ?? t.n.PlannedStartDate) })
.Where(x => x.Date >= AdminSearchVm.AdminSearch.SubmittedFrom && x.Date <= AdminSearchVm.AdminSearch.SubmittedTo)
.Select(x => x.Table);
I have the following in an MVC application:
var selectedArticles =
vm.Articles.Except(
vm.Articles.Where(x => x.Quantity == 0)).ToList();
I need to add another parameter. I DONT want to show the articles where the option HideUntilDate != NULL && HideUntilDate > todays date
Any tips?
Except not needed
var selectedArticles = vm.Articles
.Where(a => a.Quantity == 0 && !(a.HideUntilDate != null && a.HideUntilDate.Value > DateTime.Today));
Just add the requirement logic to your where clause's lambda expression
var selectedArticles =
vm.Articles.Except(
vm.Articles.Where(
x => x.Quantity == 0 ||
x.HideUntilDate == null ||
x.HideUntilDate < DateTime.Now.Date()
)
).ToList();
I have the following code:
decimal? sumFreightAmount = (db.Trips
.Where(p => p.DateTime.Month == i && p.DateTime.Year == DateTime.Now.Year)
.Sum(p => p.FreightAmount));
if (sumFreightAmount.HasValue)
{
// my actions
}
else
{
sumFreightAmount = 0;
}
I can write it the following way:
decimal sumFreightAmount = (
db.Trips
.Where(p => p.DateTime.Month == i && p.DateTime.Year == DateTime.Now.Year)
.Sum(p => p.FreightAmount) != null)
?
(db.Trips
.Where(p => p.DateTime.Month == i && p.DateTime.Year == DateTime.Now.Year)
.Sum(p => p.FreightAmount)) : 0;
but in this place I call linq extension twice (and have a long record). I have read, that I can write more shorter it in the latest version of C#, like
decimal sumFreightAmount = (db.Trips.Where(p => p.DateTime.Month == i && p.DateTime.Year == DateTime.Now.Year).Sum(p => p.FreightAmount)?.0;
but can't find it. What is the shortest record?
The best way is using GetValueOrDefault
decimal sumFreightAmount = db.Trips
.Where(p => p.DateTime.Month == i && p.DateTime.Year == DateTime.Now.Year)
.Sum(p => p.FreightAmount)
.GetValueOrDefault();
EDIT: As #IvanStoev pointed out, you can also use the null coalescing operator in this case, which is a tad bit shorter:
decimal sumFreightAmount = db.Trips
.Where(p => p.DateTime.Month == i && p.DateTime.Year == DateTime.Now.Year)
.Sum(p => p.FreightAmount) ?? 0;
I have a System.NotSupportedException using DateTime? in Linq to Entities.
My original method looks like this:
public TransportOrderLine GetLastTransportOrderLine(bool isDriver, int? driverId, int? haulierId , DateTime date, IDatabaseContext db)
{
var lines =
db.Query<TransportOrderLine>()
.Where(x => ((isDriver && x.TransportOrder.DriverId == driverId) ||
(!isDriver && x.TransportOrder.HaulierId == haulierId)) &&
x.StartDatePlanned.HasValue &&
EntityFunctions.TruncateTime(x.StartDatePlanned) <= date)
.ToList();
return lines.OrderByDescending(x => x.TransportOrder.Sequence)
.ThenByDescending(x => x.Sequence).LastOrDefault();
}
For some reason, I'm having an exception (NotSupportedException) when linq to entities executes the DateTime part.
TransportOrderLine.StartDatePlanned is of type DateTime?.
I also split my code like this already:
var lines = db.Query<TransportOrderLine>()
.Where(x => ((isDriver && x.TransportOrder.DriverId == driverId) ||
(!isDriver && x.TransportOrder.HaulierId == haulierId)))
.ToList(); //ok
lines = lines.Where(x => x.StartDatePlanned.HasValue).ToList(); //ok
lines = lines.Where(x => EntityFunctions.TruncateTime(x.StartDatePlanned)<= date)
.ToList(); //error here
I also tried this:
lines = lines.Where(x => (DateTime)EntityFunctions
.TruncateTime((DateTime)x.StartDatePlanned.Value)
.Value <= date).ToList(); //error here
Anyone knows a solution.
Thanks for your attention :)
This is my solution:
var lines =
db.Query<TransportOrderLine>()
.Where(x => ((isDriver && x.TransportOrder.DriverId == driverId) ||
(!isDriver && x.TransportOrder.HaulierId == haulierId)) &&
x.StartDatePlanned.HasValue)
.ToList().Where(x => (DateTime)x.StartDatePlanned.Value <= date);
I am trying to invalidate requests of friendship that were reponded less than 30 days ago.
var requestIgnored = context.Request
.Where(c => c.IdRequest == result.IdRequest
&& c.IdRequestTypes == 1
&& c.Accepted == false
&& DateTime.Now <= (((DateTime)c.DateResponse).AddDays(30)))
.SingleOrDefault();
c.DateResponse is of type DateTime?. The error I am having is :
LINQ does not recognize the command .AddDays
Edit: If you're using EntityFramework >= 6.0, use DbFunctions.AddDays. For older versions of Entity Framework, use EntityFunctions.AddDays:
var requestIgnored = context.Request
.Where(c => c.IdRequest == result.IdRequest
&& c.IdRequestTypes == 1
&& c.Accepted == false
&& DateTime.Now <= DbFunctions.AddDays(c.DateResponse, 30))
.SingleOrDefault();
You might try this:
var thirtyDaysAgo = DateTime.Now.AddDays(-30);
var requestIgnored = context.Request
.Where(c =>
c.IdRequest == result.IdRequest &&
c.IdRequestTypes == 1 &&
c.Accepted == false &&
c.DateResponse.HasValue &&
thirtyDaysAgo <= c.DateResponse.Value)
.SingleOrDefault();