2012-11-07 8 views
6

Oto, co chciałbym móc zrobić.Owiń zapytanie Linq w bloku try/catch, używając deklaracji metody

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

lub

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

lub

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

Zasadniczo chciałbym to dla CatchLog() na zasadzie owinąć wykonanie zapytania w catch try i Debug.WriteLine()Exception a następnie throw it.

Jakieś pomysły na to, jak mogę wprowadzić coś takiego?

Odpowiedz

8

Trzeba by przerobić swoje oświadczenie, tak jak poniżej:

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

Jeśli pozwolisz, że można napisać coś takiego:

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; 
    } 
} 
Powiązane problemy