采取以下代码
(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; }
}
}
我需要根据其他一些对象属性过滤出列表中的对象。
示例:当apple id与banana相同时,主要从列表中删除苹果** USUS EXCEPT **或其他方式
Linq让它变得麻烦。怎么做?
答案 0 :(得分:0)
lstApples.Where(a=>!lstBananas.Any(b=>b.IDBanana == a.IDApple))
或者您也可以join