采取下面的代码不同的对象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
这是一些伪代码.....为什么=而不是==准点化.....要去尝试它 –