这里是一个使用lambda加入快速去。注意我为联系对中的每个联系人使用了两个联接。 我认为这也是最有效的解决方案。
int[][] log = new int[][] {new int[]{1,2},new int[]{2,1},new int[]{1,3},new int[]{2,3},new int[]{3,4},new int[]{4,1}};
List<Suspect> Suspects = new List<Suspect>(){new Suspect(){SuspectId = 1, Name = "Bob"},new Suspect(){SuspectId = 2, Name = "Frank"},new Suspect(){SuspectId = 3, Name = "Jimmy"},new Suspect(){SuspectId = 4, Name = "DrEvil"}};
//order the contact pairs as 2 --> 1 is the same as 1 --> 2
var q = log.Select (x => x.OrderBy (o => o))
// Put contact record into an object which we have an IComparable for
.Select (s => new Contact(){A = s.ElementAt(0),B= s.ElementAt(1) })
//Now eliminate the duplicates
.Distinct(new ContactComparer())
//get the Name for contact A
.Join(Suspects, contactKey => contactKey.A, suspectKey => suspectKey.SuspectId,(c,s) => new Contact{A = c.A, AName = s.Name, B = c.B})
//get the Name for contact B
.Join(Suspects, contactKey => contactKey.B, suspectKey => suspectKey.SuspectId,(c,s) => new Contact{A = c.A, AName = c.AName, B = c.B, BName = s.Name})
.ToList();
//Classes that were used:
public class Contact
{
public int A { get; set; }
public String AName { get; set; }
public int B { get; set; }
public String BName { get; set; }
}
public class Suspect
{
public int SuspectId { get; set; }
public String Name { get; set; }
}
//We will use this in the .Distinct() linq method, to find the (and remove) the duplicates
public class ContactComparer : IEqualityComparer<Contact>
{
public bool Equals(Contact x, Contact y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
//Check whether the id fields are equal.
return x.A == y.A && x.B == y.B;
}
public int GetHashCode(Contact contact)
{
//Check whether the object is null
if (Object.ReferenceEquals(contact, null)) return 0;
//Get hash code for the Name field if it is not null.
long contactA = contact.A == null ? 0 : contact.A;
long contactB = contact.B == null ? 0 : contact.A;
//Calculate the hash code for the product.
return (int)((contactA + contactB) % int.MaxValue);
}
}
结果:
我不认为你关心的是lambda表达式。 Linq是你正在寻找的唯一标签。 – user1306322
是的,AB被视为与BA相同,IM的方向对我来说并不重要。现在唯一重要的是A和B根据IM日志已经彼此联系。我试图找出谁与谁交谈,发件人/收件人的区别对我来说并不重要。 – phan
也许你可以用更多但更简单的步骤来使用非linq表达式,比如foreach循环和if-checks?为什么它必须如此复杂? – user1306322