Get Weekday through a LINQ date

Asked

Viewed 256 times

1

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

In Sql 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

1 answer

1


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

I think it would look 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 property DayOfWeek date:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.