C# 使用linq进行验证/引发异常

C# 使用linq进行验证/引发异常,c#,.net,linq,C#,.net,Linq,这可能是错误的做事方式,但我只是想知道 我正在使用 //check that all transform fields have corresponding database columns foreach (string sDatabaseFieldName in l_sDatabaseFieldNames) { bool bFound = false; foreach (SqlParameter

这可能是错误的做事方式,但我只是想知道

我正在使用

        //check that all transform fields have corresponding database columns
        foreach (string sDatabaseFieldName in l_sDatabaseFieldNames)
        {
            bool bFound = false;
            foreach (SqlParameter sqlp in sqlcmdAll.Parameters)
            {
                if (sqlp.SourceColumn == sDatabaseFieldName) bFound = true;
            }
            if (!bFound)
            {
                throw new Exception(string.Format("Transform field {0} does not have a corresponding column in the target table.", sDatabaseFieldName));
            }
        }
其中,l_sDatabaseFieldNames是一个列表,sqlcmdAll是一个带有有效参数名的insert-SqlCommand

如果l_sDatabaseFieldNames中的项不在任何sqlcmdAll.Parameters.SourceColumn中,我想引发一个异常。换句话说,l_sDatabaseFieldNames中包含的所有列名也应该有一个匹配的参数(使用SourceColumn属性进行比较)

我也可以用

bool bFound = l_sDatabaseFieldNames.All(sDBFN => sqlcmdAll.Parameters.Cast<SqlParameter>().Any(param => sDBFN == param.SourceColumn));
boolbfound=l_sDatabaseFieldNames.All(sDBFN=>sqlcmdAll.Parameters.Cast().Any(param=>sDBFN==param.SourceColumn));
但我只得到一个正确/错误的结果

如果某个项位于l_SDatabaseFieldName中,但不在任何sqlcmdAll.Parameters.SourceColumn中,我是否可以结合使用这两种技术并从linq查询中引发异常

提前谢谢大家,,
James。

您希望它抛出什么异常?我觉得这对您来说是更好的选择,您可以获取结果并抛出您想要抛出的异常:

if (!l_sDatabaseFieldNames.All(sDBFN => sqlcmdAll.Parameters.Cast<SqlParameter>().Any(param => sDBFN == param.SourceColumn)))
{
    throw new YourCustomException("Your custom message");
}
如果您不介意编写自己的扩展方法,可以将其添加到库中(非常方便),以便将ForEach()赋予IEnumerable:

public static class EnumerableExtensions
{
    public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source,
            Action<T> action)
    {
        if (source == null) throw new ArgumentNullException("source");

        foreach (T element in source)
        {
            action(element);
        }

        return source;
    }
}

您可以选择第一个不匹配的参数

SqlParameter sqlp = sqlcmdAll.Parameters.Cast<SqlParameter>().Where(param => !l_sDatabaseFieldNames.Contains(param.SourceColumn)).FirstOrDefault();

if (sqlp != null)
    throw new Exception(string.Format("Transform field {0} does not have a corresponding column in the target table.", sqlp.SourceColumn));
SqlParameter sqlp=sqlcmdAll.Parameters.Cast().Where(param=>!l_sDatabaseFieldNames.Contains(param.SourceColumn)).FirstOrDefault();
if(sqlp!=null)
抛出新异常(string.Format(“转换字段{0}在目标表中没有相应的列。”,sqlp.SourceColumn));

谢谢您的回复。从Linq语句中抛出异常的想法意味着我可以访问param.SourceColumn,以便在异常发生时向用户提供一些额外的信息。ie'YourColumn'不是表模式的一部分。非常好的回答,谢谢。对于使用Linq语句从中抛出异常,您是否知道有任何性能惩罚,或者这是用于生产的“好”代码?干杯。而且,每次我评论你的答案时,你的答案都会变得更好。在你告诉我下周的彩票号码之前,我必须发表多少评论?:-)谢谢这更接近我想要的。。。但我想知道是否可能以某种方式从Linq表达式中抛出异常。我想我真的在问‘当条件返回false时,我能在linq语句中执行任意代码吗?’?!
public static class EnumerableExtensions
{
    public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source,
            Action<T> action)
    {
        if (source == null) throw new ArgumentNullException("source");

        foreach (T element in source)
        {
            action(element);
        }

        return source;
    }
}
        sqlcmdAll.Parameters.Cast<SqlParameter>().ForEach(p =>
            {
                if (!l_sDatabaseFieldNames.Contains(p.SourceColumn))
                {
                    throw new Exception("Could not find " + p.SourceColumn);
                }
            });
SqlParameter sqlp = sqlcmdAll.Parameters.Cast<SqlParameter>().Where(param => !l_sDatabaseFieldNames.Contains(param.SourceColumn)).FirstOrDefault();

if (sqlp != null)
    throw new Exception(string.Format("Transform field {0} does not have a corresponding column in the target table.", sqlp.SourceColumn));