LINQ语句上的自定义异常? [英] Custom Exceptions on LINQ statements?

查看:67
本文介绍了LINQ语句上的自定义异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的已编译LINQ语句:

protected static readonly Func<DataClasses1DataContext, int,int,ConfigVariable> 
getConfigVariable = CompiledQuery.Compile((DataClasses1DataContext dc, int p1, int p2) => 

解决方案

您可以编写自己的扩展方法,该方法内部仅使用FirstOrDefault.如果需要,可以解析Expressiontree以提取值如果linq查询中的每个常量/变量失败,则返回它的值.这样,您始终可以生成特定的 消息.

有关如何检查表达式树的信息,请参见此帖子: http://stackoverflow.com/questions/277150/define-an-extension-method-for-ienumerablet-which-returns-ienumerablet


I have a Compiled LINQ statement that looks like this:

protected static readonly Func<DataClasses1DataContext, int,int,ConfigVariable> 
getConfigVariable = CompiledQuery.Compile((DataClasses1DataContext dc, int p1, int p2) => 

解决方案

You could write your own Extension method that internally just uses FirstOrDefault.If you want to, you can parse the Expressiontree to extract the values of each constant/variable from the linq query if it fails. That way you can always generate a specific message.

See this post on how you can examine the expressiontree: http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx

See ths post on how to build your own extension method: http://stackoverflow.com/questions/277150/define-an-extension-method-for-ienumerablet-which-returns-ienumerablet


这篇关于LINQ语句上的自定义异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆