.net 如何使用LINQ筛选列表

tez616oj  于 2023-01-14  发布在  .NET
关注(0)|答案(3)|浏览(172)

我有一个名单下面提到。

var fakedata = new Dictionary<Gateway, List<FeMeasurementValues>>()
{
    {
        new Gateway { SiteId = 1, FirmwareVersion = "1.1.1", ConnectivityStatus = GatewayConnectivityStatus.ReadyToConnect },
                    new List<FeMeasurementValues>() { new FeMeasurementValues { MeasurementName = "MsgLRRID", Values = new List<FeValue> { new FeValue { Value = "FFFF123", Horodate = DateTime.Now } } } }
    },
    {
        new Gateway { SiteId = 2, FirmwareVersion = "1.1.2", ConnectivityStatus = GatewayConnectivityStatus.Connected },
                    new List<FeMeasurementValues>() { new FeMeasurementValues { MeasurementName = "MsgLRRID", Values = new List<FeValue> { new FeValue { Value = "GH67123", Horodate = DateTime.Now } } } }
    },
    {
        new Gateway { SiteId = 3, FirmwareVersion = "1.1.3", ConnectivityStatus = GatewayConnectivityStatus.Disconnected },
                    new List<FeMeasurementValues>() { new FeMeasurementValues { MeasurementName = "MsgLRRID", Values = new List<FeValue> { new FeValue { Value = " ", Horodate = DateTime.Now } } } }
    },
    {
        new Gateway { SiteId = 4, FirmwareVersion = "1.1.1", ConnectivityStatus = GatewayConnectivityStatus.Connected },
                    new List<FeMeasurementValues>() { new FeMeasurementValues { MeasurementName = "MsgLRRID", Values = new List<FeValue> { new FeValue { Value = "SA67123", Horodate = DateTime.Now } } } }
    }
};

我有两个办法
1."GetPublicNetworkUsedCount()"需要返回以"FFFF"开头的值的计数,因此,在这种情况下,输出应为1。
1."GetPrivateNetworkUsedCount()"需要返回不以"FFFF"开头且包含空值的值的计数。因此,在这种情况下,输出应为3。
下面是我所尝试的:

private static string GetPublicNetworkUsedCount(List<FeValue> values)
{
    var countofPublicNetwork = values.Where(x => x.Value.Any(f => x.Value.StartsWith("FFFF")));
    return countofPublicNetwork.Count().ToString();
}

private static string GetPrivateNetworkUsedCount(List<FeValue> values)
{
    var countofPrivateNetwork = values.Where(x => x.Value.Any(f => !x.Value.StartsWith("FFFF")));
    return countofPrivateNetwork.Count().ToString();
}

我得到了错误的输出,获取公共网络使用计数为0,获取私有网络使用计数为1。
请帮帮我。

fcg9iug3

fcg9iug31#

x.Value.Any()将返回真只要里面的条件是真.这导致返回1 convertet到一个数字.
要获取以FFFF开头的所有条目,请删除任何部分,例如:

var countofPublicNetwork = values.Where(x =>x.Value.StartsWith("FFFF"));

如果像Mark在他的评论中提到的那样用.Count()替换.Where(),你可以直接得到计数。

var countofPublicNetwork = values.Count(x =>x.Value.StartsWith("FFFF"));
pdsfdshx

pdsfdshx2#

你有一个List<FeValue>,其中每个对象都有一个字符串Value,你把这个字符串当作一个集合,深入一步,就不需要.Any(了。
所以支票应该是

values.Where(x => x.Value.StartsWith("FFFF")).Count();

或者只是

values.Count(x => x.Value.StartsWith("FFFF"));
fykwrbwg

fykwrbwg3#

参见备注:

//             vv I'd recommend to return int
private static string GetPublicNetworkUsedCount(List<FeValue> values)
{                                                       // vv Any doesn't make sense here: this is a string
    var countofPublicNetwork = values.Where(x => x.Value.Any(f => x.Value.StartsWith("FFFF")));
    return countofPublicNetwork.Count().ToString();
}

private static string GetPrivateNetworkUsedCount(List<FeValue> values)
{
    var countofPrivateNetwork = values.Where(x => x.Value.Any(f => !x.Value.StartsWith("FFFF")));
    return countofPrivateNetwork.Count().ToString();
}

所以,我会这样做:

private static int GetPublicNetworkUsedCount(List<FeValue> values) 
    => values.Count(x => x.Value.StartsWith("FFFF"));

private static int GetPrivateNetworkUsedCount(List<FeValue> values)
    => values.Count(x => !x.Value.StartsWith("FFFF"));

如果需要,可以将返回的int字符串化。
也许我会

public static class FeValueListExtensions
{
    public static int GetPublicNetworkUsedCount(this List<FeValue> values) 
    => values.Count(x => x.Value.StartsWith("FFFF"));

    public static int GetPrivateNetworkUsedCount(this List<FeValue> values)
    => values.Count(x => !x.Value.StartsWith("FFFF"));
}

然后可以用作

// Assume we have a List<FeValue> defined as
List<FeValue> feValues = ...
var publicCount = feValues.GetPublicNetworkUsedCount();

相关问题