System.NotSupportedException using DateTime? in Linq to Entities - c#

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);

Related

How to write a linq query to exclude some of the records?

This is my LINQ
IList<string> ExceptList = new List<string>() { "045C388E96", "C9B735E166", "02860EB192", "2401016471" };
var listusers = context.USER_INFO.Where(x => x.ACTIVATED
&& x.COMP.EQUIPMENT.Count(y => y.STATUS == (int)STATUSEQ.ACTIVE) > 0
&& (x.LAST_LOGIN < time)
&& !ExceptList.Contains(x.COMP.CODE)
&& !x.IS_LOCK
|| !x.COMP.IS_LOCK)
.Select(x => new EmailOutOfDateLoginModel
{
COMPCode = x.COMP.CODE,
First_Name = x.FIRST_NAME,
Last_Name = x.LAST_NAME,
Total_EQ = x.COMP.EQUIPMENT.Count(y => y.STATUS == (int)STATUSEQ.ACTIVE),
User_Email = x.USER_EMAIL
}).ToList();
I am not sure why my ExceptList is not working. I want to exclude any record that contaisn any of the CODE in the ExceptList
Put parentheses around the expressions containing the && logic. The || at the end is only matched with the !x.IS_LOCK || !x.COMP.IS_LOCK otherwise.
According your linq all records where (!x.COMP.IS_LOCK==true) will be included in the query. Try this "where" part:
.Where(x => x.ACTIVATED
&& x.COMP.EQUIPMENT.Count(y => y.STATUS == (int)STATUSEQ.ACTIVE) > 0
&& (x.LAST_LOGIN < time)
&& !ExceptList.Contains(x.COMP.CODE)
&& !(x.IS_LOCK && x.COMP.IS_LOCK))

Return results for closest Date if given Date does not exist in database using LINQ

I am trying to return a set of results based on a given date and if that date does not exist then then I want to return the result from the closet past date to that.
I am trying to return the results from an ApiController. The method I am using is pretty slow and I'm sure it's not the best one.
[HttpPost]
public IHttpActionResult GetItemsForDate(DateDTO Date)
{
using (var context = new CafeteriaContext())
{
bool vreauTOT = Date.vreauTOT;
var itemsList = new List<MenuItem>();
var getDates = context.MenuItems.Where(d => d.Date == Date.Date || d.Date < Date.Date).Select(d => d.Date).ToList();
var availableDate = getDates.OrderByDescending(t => t.Date).First();
if (vreauTOT)
{
itemsList = context.MenuItems
.Where(d => d.Date == availableDate)
.Select(r => r)
.ToList();
}
else
{
itemsList = context.MenuItems
.Where(d => d.Date == availableDate)
.Where(d => d.OnlyExternal == false)
.Select(r => r)
.ToList();
}
return Ok(itemsList);
}
Is it possible to save a trip to the database and maybe construct a single query that will return the same results ? Or maybe a faster way than what I am doing right now.
You probably don't need if .. else here. It can be reduced to below using compound condition
itemsList = context.MenuItems
.Where(d => d.Date == availableDate && (!vreauTOT && d => !d.OnlyExternal))
.ToList();
itemsList = context.MenuItems
.Where(d => d.Date == availableDate)
.Select(r => r)
.ToList();
No need to use Select here.
itemsList = context.MenuItems
.Where(d => d.Date == availableDate)
.Where(d => d.OnlyExternal == false)
.Select(r => r)
.ToList();
No need to use Select here.
Use 1 where and check the conditions there:
.Where(d => d.Date == availableDate && ! d.OnlyExternal)
Explanation: each LINQ-method will perform a loop in the background, and the more loops you create, the slower it will run.

Linq - Dynamic Condition

I have the following query :-
I want to add one more condition which is dynamic, so if user passes DATEOFBIRTH it should be e.DateOfBirth <= date.
var data = ctx.Employee.Where(e => e.Id == Id
&& e.Category == Category
&& e.DateOfJoining <= date)
.Select(e => e)
.ToList();
How to condition dynamically?
You can use reflection to solve this problem but there is another idea that may helps you:
var criteria = new Dictionary<string, Func<Employee, bool>>();
var date = DateTime.Now; //or any other value
//Initialize your criterias
criteria.Add("DATEOFBIRTH", e => e.DateOfBirth <= date);
criteria.Add("DateOfJoining", e => e.DateOfJoining <= date);
var selectedValue = "DATEOFBIRTH";
var data = ctx.Employee.Where(e => e.Id == id &&
e.Category == Category &&
criteria[selectedValue](e)).ToList();
So if you change the selectedValue the output will be based on corresponding criteria you are looking for.
From your comment:
If the DateOfBirth is choosen, there where condition should be appended
by one more condition e.DateOfBirth <= date.. if user chooses
DateOfAnniversary then it should be e.DateOfAnniversary <= date
Then you could use:
var data = ctx.Employee
.Where(e => e.Id == Id && e.Category == Category && e.DateOfJoining <= date);
Now, assuming that filterbyDateOfBirth and filterbyDateOfAnniversary are bools:
if(filterbyDateOfBirth)
data = data.Where(e => e.DateOfBirth <= date);
if(filterbyDateOfAnniversary)
data = data.Where(e => e.DateOfAnniversary <= date);
var list = data.ToList();
Due to LINQ's deferred execution the database is queried just once at ToList.
Sounds like you're trying to do the following:
var employees = ctx.Employee.Where(e => e.Id == Id
&& e.Category == Category
&& e.DateOfJoining <= date);
if (!string.IsNullOrWhiteSpace(DATEOFBIRTH))
{
employees = employees.Where(e => e.DateOfBirth <= DATEOFBIRTH);
}
var data = employees.ToList();
You could also do the following, which is more concise, but since it looks like you are querying a database here, I would prefer the above approach since it doesn't include anything unnecessary in the query.
var data = ctx.Employee.Where(e => e.Id == Id &&
e.Category == Category &&
e.DateOfJoining <= date &&
(string.IsNullOrWhiteSpace(DATEOFBIRTH) ||
e.DateOfBirth <= DATEOFBIRTH))
.ToList();

How to write Linq expression using external objects

I am trying to convert a loop in to a linq expression. But it seams not to work the way i am doing it:
var customer = GetCustomerFromDatabase(id);
ICollection<Order> customerOrders = null;
if (customer == null)
{
LogAndThrowCustomerNotFound(id);
}
else
{
customerOrders = customer.Orders;
}
customer.YearToDateSales = 0.0;
customer.CurrentSales = 0.0;
DateTime today = DateTime.Now;
if (customerOrders != null)
foreach (var order in customerOrders)
{
if (order.SubmittedDate != null
&& order.SubmittedDate.Value.Year.CompareTo(today.Year) == 0)
{
customer.YearToDateSales += (double)order.OrderTotal;
}
if (order.SubmittedDate != null
&& (order.SubmittedDate.Value.Month.CompareTo(today.Month) == 0
&& order.SubmittedDate.Value.Year.CompareTo(today.Year) == 0))
{
customer.CurrentSales += (double)order.OrderTotal;
}
}
So I came up with that expression to get the customer orders that match the current year... bot it does not work. in he expression order is empty and today is conflicting. I i create
DateTime today = DateTime.Now; in the parm of the expression i get different errors...
IEnumerable<Order> cOrders = customerOrders
.Where((ICollection<Order> order , today) =>
order.SubmittedDate.Value.Month == today.Month);
It's simpler if you just don't attempt pass today into the lambda, it'll be closed into the expression anyway;
customer.YearToDateSales = customerOrders
.Where(x => x.SubmittedDate != null &&
x.SubmittedDate.Value.Year == today.Year)
.Sum(x => x.OrderTotal);
customer.CurrentSales = customerOrders
.Where(x => x.SubmittedDate != null &&
x.SubmittedDate.Value.Month == today.Month &&
x.SubmittedDate.Value.Year == today.Year)
.Sum(x => x.OrderTotal);
Hard to tell exactly what's wrong without the error, but you probably need to check for null on the SubmittedDate like in the original version:
IEnumerable<Order> cOrders = customerOrders
.Where((ICollection<Order> order , today) =>
order.SubmittedDate.HasValue &&
order.SubmittedDate.Value.Month == today.Month);

Linq and work with Datetime?

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();

Categories

Resources