简体   繁体   English

我们可以在这里删除重复的字符串元素吗? 林克 C#

[英]Can we remove the repeating string elements here? Linq C#

 static IEnumerable<T> Task(IEnumerable<Supplier> supplierList,
            IEnumerable<SupplierDiscount> supplierDiscountList)
        {

            var b = supplierDiscountList.OrderBy(x => x.ShopName).ThenBy(a => a.Discount).
            Select(x => new T
            {
                ShopName = x.ShopName,
                Discount = x.Discount,
                Owner = supplierList.Where(a => a.Id == x.SupplierId).FirstOrDefault()
            }).OrderByDescending(g => g.Discount);

            return b;
            
        }

I want to remove the repeated T elements after OrderByDescending(g => g.Discount) by string shop , how can I do it and can I do it at all?我想通过字符串shop删除OrderByDescending(g => g.Discount)之后重复的T元素,我该怎么做,我可以做到吗?

supplierDiscountList is array new SupplierDiscount { Discount = 5.0, ShopName = "shop1", SupplierId = 1 }, similar objects. SupplierDiscountList 是数组 new SupplierDiscount { Discount = 5.0, supplierDiscountList new SupplierDiscount { Discount = 5.0, ShopName = "shop1", SupplierId = 1 },类似的对象。

T[] expected = new[]
new T
                    {
                        Discount = 5.0, ShopName = "shop1",
                        Owner = new Supplier{Adress = "adress 1", Id = 1, YearOfBirth = 2000}
                    },
 new T
                    {
                        Discount = 34.0, ShopName = "shop2",
                        Owner = new Supplier{Adress = "adress 2", Id = 2, YearOfBirth = 1961}
                    },
and until we run out of stores

supplierList its array of "Supplier{Adress = "adress 1", Id = 1, YearOfBirth = 2000}, .."供应商列出其“供应商{Adress = “adress 1”, Id = 1, YearOfBirth = 2000}, ..”数组

I hope I got it right我希望我做对了

 static IEnumerable<MaxDiscountOwner> Task(IEnumerable<Supplier> supplierList,
        IEnumerable<SupplierDiscount> supplierDiscountList)
    {

        return  supplierDiscountList.OrderBy(x => x.ShopName).ThenBy(a => a.Discount)
                                    .Select(x => new MaxDiscountOwner
                                    {
                                        ShopName = x.ShopName,
                                        Discount = x.Discount,
                                        Owner = supplierList.Where(a => a.Id == x.SupplierId).FirstOrDefault()
                                    })
                                    .OrderByDescending(g => g.Discount)
                                    .GroupBy(x => (x.ShopName ?? "").Trim()).OrderBy(x=>x.Key) // maybe add ToLower() yet
                                    .Select(x => x.OrderByDescending(g => g.Discount).First());           

        
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM