如何在C#中将foreach语句转换为LINQ? [英] How to convert foreach statement into LINQ in C#?

查看:70
本文介绍了如何在C#中将foreach语句转换为LINQ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i希望将foreach语句转换为C#中的LINQ ...



i有一个列表是:CandidateGroups,在这个列表中,我还有一个列表,即

canidiatelist。



这里,我m获取所有candidategroups然后比较candidategroup.candidatelist.id与当前candidiate.ID。



使用'foreach'我得到了,但我想将此foreach语句转换为LINQ。



我的尝试:



code



Hi guys,

i want to convert foreach statement into LINQ in C#...

i have one list that is: CandidateGroups, in this list, i have one more list, that is
canidiatelist.

Here, i'm fetching all candidategroups and then comparing candidategroup.candidatelist.id with current candidiate.ID.

Using 'foreach' i got, but i want to convert this foreach statement to LINQ.

What I have tried:

code

public string GroupNameAR
        {
            get
            {
                string GroupName = "";
                List[CandidateGroupInfo] CandidateGroups = CandidateGroupController.Instance.All;//i have used '[]' instead of '<>', to view code,here.
                foreach(var group in CandidateGroups)
                {
                    foreach (var candidate in group.CandidateList)
                    {
                        if (candidate.ID == ID)
                        {
                            GroupName = group.NameAR;
                        }
                    }
                }
                return GroupName != ""? GroupName.ToUpper() : null;                                               
            }
        }

推荐答案

int ID = 202;
string GroupName = string.Empty;

List<CandidateGroupInfo> CandidateGroups = new List<CandidateGroupInfo> {
    new CandidateGroupInfo{NameAR = "A", CandidateList = new List<CandidateList> { new CandidateList{ID=101}, new CandidateList{ID=102}}},
    new CandidateGroupInfo{NameAR = "B", CandidateList = new List<CandidateList> { new CandidateList{ID=201}, new CandidateList{ID=202}}},
    new CandidateGroupInfo{NameAR = "C", CandidateList = new List<CandidateList> { new CandidateList{ID=301}, new CandidateList{ID=302}}}
    };

foreach(var group in CandidateGroups)
{
    foreach (var candidate in group.CandidateList)
    {
        if (candidate.ID == ID)
        {
            GroupName = group.NameAR;
            // you should add a break as the data is found so no need
            // to continue processing in the loop
            break;
        }
    }
    // again we want to break the outer loop is the data
    // has been found
    if (!string.IsNullOrWhiteSpace(GroupName))
    {
        break;
    }
}
Console.WriteLine(GroupName != ""? GroupName.ToUpper() : null);

// now via Linq

GroupName = string.Empty;

CandidateGroupInfo info = CandidateGroups.FirstOrDefault(cg => cg.CandidateList.Any(cl => cl.ID == ID));

GroupName = info == null ? string.Empty : info.NameAR.ToUpper();


试试

try
var temp  = CandidateGroups.FirstOrDefault(k => k.CandidateList.Any(a => a.ID == ID));
           GroupName = temp != null ? temp.NameAR.ToUpper() : null;


这篇关于如何在C#中将foreach语句转换为LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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