Get day of week through a date LINQ

1

I need to get a record set of a weekday (Second Example), where for this I have a field in the table of type datetime.

In Sql it would look something like this:

SELECT AVG(P.Quant) AS Expr1
FROM     P
WHERE  (Pro.Tipo = 'B') AND (V.ID= 1)
AND EXTRACT(DOW FROM P.data) = 1
    
asked by anonymous 02.04.2014 / 19:22

1 answer

1

Use the SqlFunctions.DatePart method, which allows you to get the parts of a date within a LINQ-To-Entities query:

I think it would look like something like this:

var resultado = db.P
    .Where(x => x.Tipo == "B" && x.Id == 1)
    .Where(x => SqlFunctions.DatePart("weekday", x => x.data) == 2);

Or if it is LINQ-To-SQL, you can use the DayOfWeek property of the date:

var resultado = db.P
    .Where(x => x.Tipo == "B" && x.Id == 1)
    .Where(x => x => x.data.DayOfWeek == DayOfWeek.Monday);
    
02.04.2014 / 19:30