【.net core】在.net中可以通过linq计算第二个列表 list2中特定字段的总和,并且要求满足该字段的某个值包含在第一个列表 list1 中的任何一个值

前言

在.net中可以通过linq计算第二个列表 list2中特定字段的总和,并且要求满足该字段的某个值包含在第一个列表 list1 中的任何一个值,可以使用 LINQ 查询语句来实现

示例 

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        List<Item> list1 = new List<Item>()
        {
            new Item { Id = 1, GroupId = "A" },
            new Item { Id = 2, GroupId = "B" },
            new Item { Id = 3, GroupId = "C" }
        };

        List<OtherItem> list3 = new List<OtherItem>()
        {
            new OtherItem { ItemId = 1, Value = 10 },
            new OtherItem { ItemId = 2, Value = 20 },
            new OtherItem { ItemId = 3, Value = 30 },
            new OtherItem { ItemId = 4, Value = 40 }
        };

        var sum = list3
            .Where(item => list1.Any(listItem => listItem.GroupId.Contains(item.ItemId.ToString())))
            .Sum(item => item.Value);

        Console.WriteLine($"Sum: {sum}");
    }
}

class Item
{
    public int Id { get; set; }
    public string GroupId { get; set; }
}

class OtherItem
{
    public int ItemId { get; set; }
    public int Value { get; set; }
}

在上述示例中,我们首先创建了 list1list3 作为示例数据。然后,我们使用 Where 方法筛选出满足条件的 list3 中的元素。在筛选条件中,我们使用 list1.Any() 方法来检查 listItem.GroupId 是否包含 item.ItemId 的字符串表示形式。最后,我们使用 Sum 方法计算满足条件的元素的特定字段的总和。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在VB.NET,你可以使用LINQ查询语句或循环遍历来遍历一个List,并找到满足特定条件的元素。然后,你可以获取该元素的另一个字段。 以下是使用LINQ查询语句的示例代码: ```vb ' 假设你有一个名为Person的类,其包含字段Name和Age Public Class Person Public Property Name As String Public Property Age As Integer End Class ' 创建一个List<Person>对象并添加一些元素 Dim people As New List(Of Person)() people.Add(New Person() With {.Name = "Alice", .Age = 25}) people.Add(New Person() With {.Name = "Bob", .Age = 30}) people.Add(New Person() With {.Name = "Charlie", .Age = 35}) ' 定义要查找的字段 Dim targetName As String = "Bob" Dim targetAge As Integer ' 使用LINQ查询语句找到满足条件的元素,并获取一个字段 Dim result = From person In people Where person.Name = targetName Select person.Age ' 检查是否找到了匹配的元素 If result.Any() Then targetAge = result.First() Console.WriteLine("Found {0} with age {1}", targetName, targetAge) Else Console.WriteLine("No match found for {0}", targetName) End If ``` 在上述示例代码,我们创建了一个包含Person对象的List,并定义了要查找的字段。然后,我们使用LINQ查询语句从List筛选出满足条件的元素,并选择另一个字段。如果找到了匹配的元素,我们将其赋给`targetAge`变量,并进行相应的操作。 你也可以使用循环遍历的方法来实现相同的功能: ```vb For Each person In people If person.Name = targetName Then targetAge = person.Age Console.WriteLine("Found {0} with age {1}", targetName, targetAge) Exit For End If Next If targetAge = 0 Then Console.WriteLine("No match found for {0}", targetName) End If ``` 这里使用了`For Each`循环遍历List的元素,判断是否满足条件,并获取一个字段。如果找到了匹配的元素,我们将其赋给`targetAge`变量,并进行相应的操作。 请根据你的具体情况选择适合的方法来遍历List并找到相应的。希望对你有所帮助!如有任何进一步问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一起来学吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值