2012-07-17 10 views
5

私はDateTimeのプロパティを持つBillingsというエンティティを持っています。私は一意に日付を見つける必要があるので、私はそれらを通過していくつかのことをすることができます。 は、私が試した:Linqとの別の日付

var DistinctYearMonthsDays = (from t in db.Billings 
           where t.DateTime.HasValue 
           select t.DateTime.Value.Date).Distinct(); 

foreach (var day in DistinctYearMonthsDays) 

が、私は(foreach上)を取得:

指定された型のメンバ '日付' はエンティティへのLINQでサポートされていません。 イニシャライザ、エンティティメンバ、およびエンティティナビゲーションプロパティ のみがサポートされています。すべてのヘルプは非常に高く評価されます

IEnumerable<DateTime> DistinctYearMonthsDays = db.Billings 
    .Select(p => new 
     {    
      p.DateTime.Value.Date.Year, 
      p.DateTime.Value.Date.Month, 
      p.DateTime.Value.Date.Day 
     }).Distinct() 
     .ToList() 
     .Select(x => new DateTime(x.Year,x.Month,x.Day)); 

検索した後、私も成功せず、次の試してみました。

+0

[this] [1]ページを見ましたか? [1]:http://stackoverflow.com/questions/5289338/linq-query-distinct-date –

答えて

8

組み込みのEntityFunctionを使用できます。あなたのクエリはなる:EntityFunctionsの詳細については、MSDNをチェック

var DistinctYearMonthsDays = (from t in db.Billings 
           where t.DateTime.HasValue 
           select EntityFunctions.TruncateTime(t.DateTime)).Distinct(); 

+0

非常にあなたの迅速な(良い)応答ジェフ – user1531996

+0

感謝をありがとう! –