linq 如何使用C#在.NET Core中删除嵌套列表中的项?

stszievb  于 2023-07-31  发布在  C#
关注(0)|答案(3)|浏览(121)

我必须使用C#从嵌套列表中删除一个项。这是我的清单:

{
    "Name": "CC",
    "Categories": [
        { "description": "Visaa", "payCode": "v" },
        { "description": "Master", "payCode": "m" }
    ]
},
{
    "Name": "paypal",
    "Categories": [
        { "description": "paypal", "payCode": "p" }
    ]
}

字符串
我想从CC类型中删除此类别列表:

{
   "description": "Master",
   "payCode": "m"
}


在那之后,我的列表应该看起来像这样:

{
        "Name": "CC",
        "Categories": [
            { "description": "Visaa", "payCode": "v" }
        ]
 },
 {
        "Name": "paypal",
        "Categories": [
            { "description": "paypal", "payCode": "p" }
        ]
 }


我尝试了下面的方法,但不确定如何做到,以得到上面的列表

payments.Find(x => x.Name == "CreditCard").PaymentOptions;

4c8rllxm

4c8rllxm1#

payments.Find(item => item.name == "CC")
        .Categories.Remove(cat => cat.payCode == 'm');

字符串

gz5pxeao

gz5pxeao2#

你需要从categories对象中删除元素,这里有一个例子...

using Newtonsoft.Json;

namespace tests
{
    public class Testing
    {
        public void Run()
        {

            var stringPayments = "[{\"Name\":\"CC\",\"Categories\":[{\"description\":\"Visaa\",\"payCode\":\"v\"},{\"description\":\"Master\",\"payCode\":\"m\"}]},{\"Name\":\"paypal\",\"Categories\":[{\"description\":\"paypal\",\"payCode\":\"p\"}]}]";
            var listPayments = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Payment>>(stringPayments);
            
            //List before remove
            Console.WriteLine("List before remove");
            PrintPayments(listPayments);

            //List after remove
            listPayments.Find(p => p.Name == "CC")
                            .Categories.RemoveAll(c => c.Description == "Master");
                            
            Console.WriteLine("\nList after remove");
            PrintPayments(listPayments);
            
        }

        public void PrintPayments(List<Payment> listPayments)
        {
            Console.WriteLine("---------Start-----------");
            foreach (var item in listPayments)
            {
                Console.WriteLine("- Name: " + item.Name);
                Console.WriteLine("---- Categories ----");
                foreach (var category in item.Categories)
                {   
                    Console.WriteLine("---- Description: " + category.Description + ", Paycode: " + category.Paycode);
                }
                
            }
            Console.WriteLine("---------End------------");
        }

    }

    public class Payment
    {
        public string Name { get; set; }
        public List<PaymentCategory> Categories { get; set; }
    }

    public class PaymentCategory
    {
        public string Description { get; set; }
        public string Paycode { get; set; }
    }
}

字符串

ubbxdtey

ubbxdtey3#

LINQ语句只能查询数据,不能更改查询到的数据

如果要更改本地内存中的数据,必须获取数据的引用,然后枚举获取的引用以更改它。
如果数据在其他地方,例如在数据库中,则需要特殊的方法来更改数据。如果使用Entity Framework,则必须查询完整的项(=不使用Select),枚举获取的数据以更改数据,然后调用SaveChanges。

回到你的问题

当你使用Find时,我假设你使用的是类似Entity Framework的东西。
你忘了给予我们上课。我认为你有类似于以下的类:

class PaymentCategory
{
    public string Name {get; set;}
    public string PayCode {get; set;}      // or even better: enum?
}

class PaymentMethods
{
    public string Name {get; set;}
    public virtual ICollection<PaymentCategory> Categories {get; set;}
}

字符串

**要求:**给定PaymentMethods X和一个PaymentCategory Y的序列,给出所有PaymentMethods X的序列,属性Categories中不包含PaymentCategory Y。

不如这样:

IQueryable <PaymentMethod> paymentMethods = ...
PaymentCategory categoryToRemove = ...

IEnumerable<PaymentMethod> paymentMethodsWithoutCategory =
    paymentMethods.Select(paymentMethod => new PaymentMethod
    {
        Name = paymentMethod.Name,

        // keep only those PaymentCategories that have not Name / PayCode
        // equal to the ones in categoryToRemove
        Categories = paymentMethod.Categories
            .Where(category => category.Name != categoryToRemove.Name
                            || category.PayCode != categoryToRemove.PayCode)
            .ToList(),
     });


换句话说:从paymentMethods序列中的每个paymentMethod中,创建一个新的paymentMethod,其中包含以下属性值:

*名称:使用paymentMethod的名称
*分类:从paymentMethod中的Categories序列中获取每个paymentCategory,这些类别的Name或Paycode与categoryToRemove中的Name / Paycode不同。把这个列个表。

相关问题