1

私のSQLクエリは次のとおりです。

select count(*),CONVERT(varchar, AddedOn, 101) 
from MemberNotifications where IsActive=1  
group by CONVERT(varchar, AddedOn, 101) 
order by CONVERT(varchar, AddedOn, 101) desc

しかし、次の試みで結果を得ることができません

    List<NotificationCounts> lst =
(from mn in this.MemberNotifications 
 where  mn.UId.Equals(friendId) 
select new NotificationCounts{ NotificationDate = mn.AddedOn.ToString("mm/dd/yyyy") })
.ToList<NotificationCounts>();

マットの文字列の日付のリストのみを取得したいのですが、例外が発生しています

LINQ to Entities はメソッド 'System.String ToString(System.String)' メソッドを認識せず、このメソッドはストア式に変換できません。

このエラーの解決策はありますか?

4

4 に答える 4

0

SQL への変換がないため、そこで ToString() を呼び出すことはできません。最初に呼び出すToList()必要があり、その後、必要に応じてメモリ内オブジェクトを操作できます。

List<NotificationCounts> lst =
    (from mn in this.MemberNotifications 
     where  mn.UId.Equals(friendId) 
     select 
       new { 
            NotificationDate = mn.AddedOn
       })
    .ToList()
    .Select(n => new NotificationCounts 
                  {
                      NotificationDate  = n.NotificationDate.ToString("mm/dd/yyyy")
                  });

編集: 日付によるグループ化については、この質問を参照してください: LINQ to Entities group-by failure using .date 要するに: EntityFunctions.TruncateTime メソッドを使用します。

于 2013-05-15T07:51:14.900 に答える
0

true... linq は .ToString を SQL 命令に変換できません... mn.AddedOn のプレーンな値を取得し、データベースの取得後に変換する必要があります。それが役に立てば幸い

于 2013-05-15T07:47:26.630 に答える
0

これを使用します(テストされていません)

List<NotificationCounts> lst = (from mn in this.MemberNotifications 
                                where  mn.UId.Equals(friendId) select mn).ToList()
                               .Select(mn=>new NotificationCounts
                               { 
                                  NotificationDate = mn.AddedOn.ToString("mm/dd/yyyy") 
                               }).ToList();
于 2013-05-15T07:57:20.877 に答える