C# Lamba With Datetime and Between

Equivalent of SQL Between Statement Using Linq or a Lambda expression

Something like this?

var query = from l in list
where l.DateValue >= new DateTime(2010, 1, 1)
&& l.DateValue <= new DateTime(2011, 1, 1)
select l;

You can write your own extension method:

public static bool IsBetween(this DateTime dt, DateTime start, DateTime end)
{
return dt >= start && dt <= end;
}

In which case the query would look something like (method syntax for a change):

var start = new DateTime(2010, 1, 1);
var end = new DateTime(2011, 1, 1);
var query = list.Where(l => l.DateValue.IsBetween(start, end));

I see you've provided some samples with the dates as strings. I would definitely keep the parsing logic (DateTime.ParseExactor other) separate from the query, if at all possible.

c# lambda get list of task between two dates

"Let's say that the user wants to view all results that's is available for pickup between the 01-03-2018 and 14-03-2018"

From your comment below, it sounds like you want to select all the tasks where either the AvailableDate or the PickupDate are between the PickupDateFrom (01-03-2018) the PickupDateTo (14-03-2018):

var availableTasks = tasks.Where(task => 
(task.AvailableDate.GetValueOrDefault() >= PickupDateFrom &&
task.AvailableDate.GetValueOrDefault() <= PickupDateTo) ||
(task.PickupDate.GetValueOrDefault() <= PickupDateFrom &&
task.PickupDate.GetValueOrDefault() <= PickupDateTo).ToList();

Lambda join between date range

You can use multiple from clauses to achieve a cross join and then filter on the conditions:

from we in _context.WeekEnding
from dbs in _context.DBS
where dbs.ResultDateTime >= we.ResultDateTime
&& dbs.ResultDateTime <= we.EndDateTime
select new (we, dbs)

c# dynamic lambda expression comparing by date not by DateTime

Well, think of the logic. For greater than:

e.createdOn > 2016-06-06

Is exactly the same as querying (notice the 7):

e.createdOn >= 2016-06-07 00:00:00.000

So, for greater than just add one day to the filter date:

var dateexpr = Expression.Constant(
Convert.ToDateTime(filter.FilterValue).Date.AddDays(1),
typeof(DateTime)
);

For less than it'd work with your original expression.

And for equals you'd need to compose the expression:

e.createdOn >= (your_dateexpression) && e.createdOn < (the expression with one more day)

I'll let you figure out less than or equal and greater than or equal, but just thinking of the logic would do :-)

Convert DateTime format to MMMM yyyy, dd using lambda expression

You could try to fetch your records as it is by AsEnumerable and then apply date formatting on projected records like

select new
{
o.panumber,
b.legacycode,
b.lastname,
b.firstname,
b.phone1,
p.providername,
a.txndate //as it is
})
.AsEnumerable()
// now convert
.Select(a => new
{
panumber = a.panumber,
legacycode = a.legacycode,
lastname = a.lastname,
firstname = a.firstname,
phone1 = a.phone1,
providername = a.providername,
txndate = a.txndate.Value.ToString("MMMM yyyy, dd")
});

Need lambda expression OrderBy with DateTime conversion

Rather gross and inefficient:

List<MyObject> orderedList = unorderedList.OrderBy(p => DateTime.Parse(p.Details.Find(s => s.Name == sortColumn).Value)).ToList();

To reduce the number of lookups/parsing:

List<MyObject> orderedList =
(from extracted in (from p in unorderedList
select new { Item = p, Date = DateTime.Parse(p.Details.Find(s => s.Name == sortColumn).Value })
orderby extracted.Date
select extracted.Item)
.ToList();

Declaring literal dates in lambda expression

Assuming you really mean you want to generate an appropriate expression tree with a ConstantExpression in, I think you'll have trouble doing that with an actual lambda expression. However, you could construct the relevant expression tree reasonably easily by constructing an expression tree for the rest of it with a lambda expression, and doing the rest manually. Here's a short but complete example:

using System;
using System.Linq.Expressions;

public class Model
{
public string Name { get; set; }
public DateTime Date { get; set; }
}

public class Test
{
public static void Main()
{
Expression<Func<Model, bool>> original = x => x.Name == "Fred";
var parameter = original.Parameters[0];
var dateClause = Expression.LessThanOrEqual(
Expression.Property(parameter, "Date"),
Expression.Constant(new DateTime(2015, 1, 1), typeof(DateTime)));
var combined = Expression.AndAlso(original.Body, dateClause);
var tree = Expression.Lambda<Func<Model, bool>>(combined, parameter);
Console.WriteLine(tree);
}
}

Output:

x => ((x.Name == "Fred") AndAlso (x.Date <= 01/01/2015 00:00:00))

You could fairly easily build this into a utility method if you need it in multiple places.



Related Topics



Leave a reply



Submit