Andy Johnston
Andy Johnston

Reputation: 487

How to convert this Sql to Linq

How would I write this sql as a C# Linq to Sql statement?

DECLARE @RET DATETIME

SET @RET = (SELECT MAX(EffectiveDate)
            FROM Material_Price
            WHERE Material_ID = @MatID
            AND EffectiveDate <= GetDate())

(It just isn't coming to me tonight)

Upvotes: 0

Views: 36

Answers (2)

Enigmativity
Enigmativity

Reputation: 117064

I would tend to do something like this:

var time = db
    .Material_Price
        .Where(m => m.Material_ID == MatID)
        .Where(m => m.EffectiveDate <= DateTime.Today)
        .OrderByDescending(m => m.EffectiveDate)
        .First();

Upvotes: 0

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

var time = db.Material_Price.Where(m => m.Material_ID == MatID
                                        && m.EffectiveDate <= DateTime.Today)
                            .Max(m => m.EffectiveDate);

Upvotes: 1

Related Questions