採取下面的代碼不同的對象LINQ比較和excude /包括基於類似的類型屬性
(在LINQ部分的僞代碼 - > lstApples.Except(I => i.id = Banana.ID) ;)
using System.Collections.Generic;
using System.Linq;
delegate int NumberChanger(int n);
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Banana> lstBananas = new List<Banana>()
{
new Banana {IDBanana = 1, Name = "Mike", Surname = "x" },
new Banana {IDBanana = 2, Name = "Mauro", Surname = "y" },
new Banana {IDBanana = 3, Name = "Ciccio", Surname = "z" },
new Banana {IDBanana = 4, Name = "Enzo", Surname = "w" }
};
List<Apple> lstApples = new List<Apple>()
{
new Apple {IDApple = 1, Brand = "Melinda", Type = "Golden" },
new Apple {IDApple = 1, Brand = "Melinda", Type = "Tirolese" },
new Apple {IDApple = 1, Brand = "Melinda", Type = "d" },
new Apple {IDApple = 1, Brand = "Melinda", Type = "f" },
new Apple {IDApple = 1, Brand = "Secure", Type = "r" },
new Apple {IDApple = 1, Brand = "Melinda", Type = "g" },
new Apple {IDApple = 1, Brand = "AllOver", Type = "Tirolese" },
new Apple {IDApple = 1, Brand = "Valtellina", Type = "Rossa" },
new Apple {IDApple = 1, Brand = "ThisOne", Type = "Verde" },
new Apple {IDApple = 1, Brand = "Melinda", Type = "Blu" },
};
lstApples.Except(i => i.id = Banana.ID);
}
}
public class Banana
{
public int IDBanana { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
}
public class Apple
{
public int IDApple { get; set; }
public string Brand { get; set; }
public string Type { get; set; }
}
}
我需要基於其他對象屬性過濾掉列表中的對象。
例如:當蘋果ID是一樣的香蕉,從列表中切出蘋果**使用實施**爲主,或其他方式,如不可能
的LINQ使繁瑣。怎麼做?
首先,你必須 「=」,而不是 「==」。通常,當我比較兩個長度相等的數組時,我使用「其中((x,i)=>」或「Select((x,i)=>」,其中x是值,i是值的索引所以我的代碼看起來像lstBananas.Where((x,i)=> x == lstApples [i]) – jdweng
這是一些僞代碼.....爲什麼=而不是==準點化.....要去嘗試它 –