循环转换为LINQ [英] loop conversion into LINQ

查看:90
本文介绍了循环转换为LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下代码转换为LINQ,请帮我解决此问题。



I want to convert following code into LINQ please help me regarding this.

foreach (ClaimRequestField row in templateMetaData.Metadata)
                {

                   if (arrRequriredFieldList.Contains(row.Name) && row.Value == null)
                    {
                       isValid = false;
                       validationErrList.Add(string.Format(ImageBasedMessages.Required_FieldName_Err,row.Name));
                    }
                   if (row.Name.ToLower() == lfConfig.NetClaimAmount.ToLower())
                   {
                       if (Convert.ToInt32(row.Value) <= 0)
                       {
                            isValid = false;
                            validationErrList.Add(string.Format(ImageBasedMessages.NetClaimAmount_Value_Err, row.Name));
                       }

                   }
                }

推荐答案

我会说,不要做到这一点。有多个条件 - 这种代码不适合用LINQ表达。虽然它肯定有可能以某种方式它不会获得任何东西。 LINQ默认情况下不是更好。它是一种语言功能,有利于查询,分组,排序,预测...但不一定非常适合区分大小写。它会使你的代码更难阅读。
I would say, don't do it. There are multiple conditionals - that kind of code doesn't lend itself very good to being expressed with LINQ. While it surely is possible somehow it wouldn't gain you anything. LINQ isn't better by default. It's a language-feature that is good for querying, grouping, sorting, projecting... but not very good for case-distinctions. It would make your code much harder to read.


首先,请阅读我对这个问题的评论。



As根据我的理解,你想要收集错误。

First of all, please read my comment to the question.

As per my understanding, you want to get collection of errors.
var errorlist = from md in templateMetaData.Metadata
    select new
    {
        errorno = (int)(md.Value) ?? 0,  //replaces null with 0
        isvalid = (int)(md.Value ?? 0) < =0 ? false : true
        message = md.Value != null ? string.Format(ImageBasedMessages.NetClaimAmount_Value_Err, md.Name) : string.Format(ImageBasedMessages.Required_FieldName_Err, md.Name)
    };





注意:这是我最好的猜测!



Note: it's my best guess!


这篇关于循环转换为LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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