包含()以及如何实现它

时间:2022-12-13 20:50:57

Say a class

说一堂课

Person  
+Name: string  
+Contacts: List<Person>

I want to be able to check if a person has a contact with a certain name without having to create a dummy Person instance.

我希望能够检查某人是否具有某个名称的联系人,而无需创建虚拟Person实例。

person.Contacts.Contains<string>("aPersonName");

This should check all persons in the Contacts list if their Name.Equals("aPersonName"); I see that there is a Contains already available, but I don't know where I should implement it's logic.

这应该检查联系人列表中的所有人是否他们的Name.Equals(“aPersonName”);我看到有一个Contains已经可用,但我不知道我应该在哪里实现它的逻辑。

5 个解决方案

#1


12  

It's probably easiest to use Enumerable.Any:

使用Enumerable.Any可能最简单:

 return person.Contacts.Any(person => person.Name=="aPersonName");

Alternatively, project and then contain:

或者,项目然后包含:

 return person.Select(person => person.Name).Contains("aPersonName");

#2


2  

I'm assuming the Contacts is the Contacts for the person in question (person in your code snippit)

我假设联系人是相关人员的联系人(代码snippit中的人)

List has a contains method that takes an object of type T as a parameter and returns true or false if that object exists in the list. What your wanting is IList.Exists method, Which takes a predicate.

List有一个contains方法,它将类型为T的对象作为参数,如果该对象存在于列表中,则返回true或false。你想要的是IList.Exists方法,它采用谓词。

example (c# 3.0)

例子(c#3.0)

bool hasContact = person.Contacts.Exists(p => p.Name == "aPersonName");

or (c# 2.0)

或(c#2.0)

bool hasContact = person.Contacts.Exists(delegate(Person p){ return p.Name == "aPersonName"; });

#3


1  

I'd agree with Jon's Any, but if you are stuck with C# 2.0, or C# 3.0 with .NET 2.0/3.0 and no LINQBridge, then another approach is List<T>.Find or List<T>.Exists. I'll illustrate with Find, since Exists got posted just as I was about to hit the button ;-p

我同意Jon的Any,但是如果你坚持使用C#2.0,或者使用.NET 2.0 / 3.0而没有LINQBridge的C#3.0,那么另一种方法是List .Find或List .Exists。我将用Find来说明,因为Exists在我即将按下按钮时被发布了; -p

// C# 2.0
bool knowsFred = person.Contacts.Find(delegate(Person x) { return x.Name == "Fred"; }) != null;
// C# 3.0
bool knowsFred = person.Contacts.Find(x => x.Name == "Fred") != null;

#4


0  

The solutions already presented by Jon Skeet and yapiskan are the way to go. If you need exactly what you're stating then you could write an extension method:

Jon Skeet和yapiskan已经提出的解决方案是可行的方法。如果你确切地需要你所说的话,你可以写一个扩展方法:

public static class ContactListExtensions
{
    public static bool Contains<T>(this List<Person> contacts, string aPersonName)
    {
        //Then use any of the already suggested solutions like:
        return contacts.Contains(c => c.Name == aPersonName);
    }
}  

#5


0  

You could create the extension method

您可以创建扩展方法

public static bool Contains(this IList<Person> list, string name) {
    return list.Any(c => c.Name == name);
}

#1


12  

It's probably easiest to use Enumerable.Any:

使用Enumerable.Any可能最简单:

 return person.Contacts.Any(person => person.Name=="aPersonName");

Alternatively, project and then contain:

或者,项目然后包含:

 return person.Select(person => person.Name).Contains("aPersonName");

#2


2  

I'm assuming the Contacts is the Contacts for the person in question (person in your code snippit)

我假设联系人是相关人员的联系人(代码snippit中的人)

List has a contains method that takes an object of type T as a parameter and returns true or false if that object exists in the list. What your wanting is IList.Exists method, Which takes a predicate.

List有一个contains方法,它将类型为T的对象作为参数,如果该对象存在于列表中,则返回true或false。你想要的是IList.Exists方法,它采用谓词。

example (c# 3.0)

例子(c#3.0)

bool hasContact = person.Contacts.Exists(p => p.Name == "aPersonName");

or (c# 2.0)

或(c#2.0)

bool hasContact = person.Contacts.Exists(delegate(Person p){ return p.Name == "aPersonName"; });

#3


1  

I'd agree with Jon's Any, but if you are stuck with C# 2.0, or C# 3.0 with .NET 2.0/3.0 and no LINQBridge, then another approach is List<T>.Find or List<T>.Exists. I'll illustrate with Find, since Exists got posted just as I was about to hit the button ;-p

我同意Jon的Any,但是如果你坚持使用C#2.0,或者使用.NET 2.0 / 3.0而没有LINQBridge的C#3.0,那么另一种方法是List .Find或List .Exists。我将用Find来说明,因为Exists在我即将按下按钮时被发布了; -p

// C# 2.0
bool knowsFred = person.Contacts.Find(delegate(Person x) { return x.Name == "Fred"; }) != null;
// C# 3.0
bool knowsFred = person.Contacts.Find(x => x.Name == "Fred") != null;

#4


0  

The solutions already presented by Jon Skeet and yapiskan are the way to go. If you need exactly what you're stating then you could write an extension method:

Jon Skeet和yapiskan已经提出的解决方案是可行的方法。如果你确切地需要你所说的话,你可以写一个扩展方法:

public static class ContactListExtensions
{
    public static bool Contains<T>(this List<Person> contacts, string aPersonName)
    {
        //Then use any of the already suggested solutions like:
        return contacts.Contains(c => c.Name == aPersonName);
    }
}  

#5


0  

You could create the extension method

您可以创建扩展方法

public static bool Contains(this IList<Person> list, string name) {
    return list.Any(c => c.Name == name);
}