The name of the class
Zhang SAN 1
Zhang SAN 1
Li si 2
Zhang SAN 2
Zhang SAN 2
Expected results:
The name of the class
Zhang SAN 1
Zhang SAN 1
Li si 2
Zhang SAN 2
Class 1 case XiaBuQu weighs only to class for 2 to heavy, for such a linq statement
CodePudding user response:
The union,CodePudding user response:
public static void Main (string [] args)
{
Var list=new List() {
New User () {Name="* *", the Age=1},
New User () {Name="* *", the Age=1},
New User () {Name="bill", the Age=2},
New User () {Name="* *", the Age=2},
New User () {Name="* *", the Age=2},
};
Var q=list. The Where (x=& gt; X.A ge==1). The Union (the list. The Where (x=& gt; X.A ge==2). The Distinct (new userCompare ())). ToList ();
Q.F orEach (x=& gt; Console. WriteLine (x.N ame + ":" + x.A ge));
Console.ReadLine();
}
The class userCompare: IEqualityComparer
{
Public Boolean Equals (User x, the User y)
{
Return x.N ame. Equals (y.N ame) & amp; & X.A ge. Equals (y.A ge);
}
Public int GetHashCode (User obj)
{
Return (obj. Name + obj. Age). GetHashCode ();
}
}
Public class User
{
Public int ID {get; set; }
Public string Name {get; set; }
Public int the Age {get; set; }
Public bool? IsChild {get; set; }
Public User Clone ()
{
Return (User) enclosing MemberwiseClone ();
}
}
CodePudding user response:
A whopping linq zones,CodePudding user response:
With a distinctCodePudding user response:
Lambda condition 1 union 2 to heavy, no trouble, thinking is such a train of thought, how to write about the samevar q=(
The from c in the list
Where Arthur c. lass==1
Select new {c.N ame, Arthur c. lass}
). The Concat (
(from c in the list
Where Arthur c. lass==2
Select new {c.N ame, Arthur c. lass}) Distinct ());