加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用方法声明在try / catch块中包装Linq查询

发布时间:2020-12-15 04:26:28 所属栏目:百科 来源:网络整理
导读:所以这就是我希望能够做到的. 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()基
所以这就是我希望能够做到的.
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()基本上将查询的执行包装在try catch中,而Debug.WriteLine()则是Exception,然后抛出它.

关于如何实现这样的事情的任何想法?

解决方法

您需要重写您的语句,如下所示:
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;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读