2012-11-07 15 views
6

ここで私は何をしたいのですか?メソッド宣言を使用してtry/catchブロックにLinqクエリをラップします

var a = Item.CatchLog().Where(x=>x.Property=="value").Take(10); 

または

var a = Item.CatchLog().FirstOrDefault(x=>x.Property=="value"); 

または

var a = Item.CatchLog().Any(x=>x.Property=="value"); 

基本的に、私は希望基本的にトライキャッチでのクエリの実行をラップするCatchLog()ためのもので、Debug.WriteLine()Exception、その後、 throwです。

私はこのようなものをどのように実装できるかに関するアイデアはありますか?

答えて

8

あなたがそうのように、あなたの文を書き直す必要があります:

var a = Item.CatchLog(c => c.Where(x => x.Property=="value").Take(10)); 

あなたは、あなたのような何かを書くことができることを許可する場合:

public static U CatchLog<T,U>(this IEnumerable<T> collection, Func<IEnumerable<T>,U> method) 
{ 
    try 
    { 
     return method(collection); 
    } 
    catch(Exception e) 
    { 
     Debug.WriteLine(e.Message); 
     throw; 
    } 
} 
関連する問題