2012-11-07 6 views
6

İşte burada yapmak istediğim şey buydu. Bir yöntem açıklaması kullanarak bir try/catch bloğundaki bir Linq sorgusunu sarın

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

veya

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

Esasen

, ben istiyorum

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

veya

sonra Exception ve temelde bir deneyin catch sorgunun çalıştırılmasına sarmak CatchLog() içindir ve Debug.WriteLine() throw

Böyle bir şeyi nasıl uygulayabileceğime dair herhangi bir fikir var mı?

cevap

8
Sen şöyle, ifadenizi yeniden yazmak gerekir

:

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

Eğer böyle bir şey yazabilirim o izin verirseniz:

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; 
    } 
} 
İlgili konular