Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将文件循环转换为LINQ?_C#_Linq_For Loop - Fatal编程技术网

C# 如何将文件循环转换为LINQ?

C# 如何将文件循环转换为LINQ?,c#,linq,for-loop,C#,Linq,For Loop,我有一个方法如下: public static GatewayClient GetCurGwClient(System.Net.IPAddress gwIP) { for (int i = 0; i < listKateApiObjcts.Count; i++) { if (listKateApiObjcts[i].CurrentGatewayClient.Gateway.IpAddress.ToString() == gwIP.T

我有一个方法如下:

public static GatewayClient GetCurGwClient(System.Net.IPAddress gwIP)
{
    for (int i = 0; i < listKateApiObjcts.Count; i++)
    {
        if (listKateApiObjcts[i].CurrentGatewayClient.Gateway.IpAddress.ToString() == 
            gwIP.ToString())
        {
            return listKateApiObjcts[i].CurrentGatewayClient;
        }
    }

    return null;
}
我想使用LINQ来简化这段代码,但我无法正确地完成它。

返回满足特定条件的第一个元素。如果未找到满足条件的元素,则对于引用类型,类型的默认值将返回null。您的代码如下所示:

public static GatewayClient GetCurGwClient(System.Net.IPAddress gwIP)//string
{
    return listKateApiObjcts
      .FirstOrDefault(x => x.CurrentGatewayClient.Gateway.IpAddress.ToString() == gwIP.ToString())
     ?.CurrentGatewayClient;
}
返回满足特定条件的第一个元素。如果未找到满足条件的元素,则对于引用类型,类型的默认值将返回null。您的代码如下所示:

public static GatewayClient GetCurGwClient(System.Net.IPAddress gwIP)//string
{
    return listKateApiObjcts
      .FirstOrDefault(x => x.CurrentGatewayClient.Gateway.IpAddress.ToString() == gwIP.ToString())
     ?.CurrentGatewayClient;
}
您正在寻找:

您正在寻找:


返回listKateApiObjcts.FirstOrDefaultitem=>item.CurrentGatewayClient.Gateway.IpAddress.ToString==gwIP.ToString?.CurrentGatewayClient;返回listKateApiObjcts.FirstOrDefaultitem=>item.CurrentGatewayClient.Gateway.IpAddress.ToString==gwIP.ToString?.CurrentGatewayClient;我无法使用此实现调用ListKateApioObjects。我无法使用此实现调用ListKateApioObjects。