15

私はこのコードを持っています:

from pr in e.ProgramSetup.Include("Program").Include("Program.Client")
        where pr.DateBegin < DateTime.Now
        && pr.DateEnd > DateTime.Now.AddDays(pr.DateEndOffset) 
select pr).ToList();

AddDays() は SQL の生成に使用できないため、機能しません。

では、別の方法はありますか?今、私はすべてを選択し、最終的に foreach でフィルタリングしますが、私の意見では良い方法ではありません。

問題は、pr.DateEndOffset も db のみにあり、定数ではないことです...

4

2 に答える 2

10

標準関数にマップされた EntityFunctions の 1 つを使用する必要があります。AddDays の例を次に示します。

public class MyEntity
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<MyEntity> Entities { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        using (var ctx = new MyContext())
        {
            if (!ctx.Entities.Any())
            {
                ctx.Entities.Add(new MyEntity() { Date = new DateTime(2000, 1, 1) });
                ctx.Entities.Add(new MyEntity() { Date = new DateTime(2012, 10, 1) });
                ctx.Entities.Add(new MyEntity() { Date = new DateTime(2012, 12, 12) });
                ctx.SaveChanges();
            }

            var q = from e in ctx.Entities
                    where e.Date > EntityFunctions.AddDays(new DateTime(2012, 10, 1), 10)
                    select e;

            foreach (var entity in q)
            {
                Console.WriteLine("{0} {1}", entity.Id, entity.Date);
            }
        }
    }
}
于 2012-10-01T17:27:15.563 に答える