score:12
It's probably easiest to use Enumerable.Any:
return person.Contacts.Any(person => person.Name=="aPersonName");
Alternatively, project and then contain:
return person.Select(person => person.Name).Contains("aPersonName");
score:0
You could create the extension method
public static bool Contains(this IList<Person> list, string name) {
return list.Any(c => c.Name == name);
}
score: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:
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);
}
}
score: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
// 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;
score:2
I'm assuming the Contacts is the Contacts for the person in question (person in your code 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.
example (c# 3.0)
bool hasContact = person.Contacts.Exists(p => p.Name == "aPersonName");
or (c# 2.0)
bool hasContact = person.Contacts.Exists(delegate(Person p){ return p.Name == "aPersonName"; });
Source: stackoverflow.com
Related Query
- What does this C# code with an "arrow" mean and how is it called?
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- How to implement Unit of Work that works with EF and NHibernate
- How can I code an outer join using LINQ and EF6?
- Contains<T>() and how to implement it
- LINQ: Paging technique, using take and skip but need total records also - how to implement this?
- How to get and use source of grouping in LINQ?
- how to implement join using LINQ and EntityFramework
- How to change the precision and the scale of decimal globally through code first?
- How to implement generic method approach using Linq and XML
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How to implement First and FirstOrDefault for boolinq?
- How to implement NextRcord and PreviousRecord button using Linq?
- Are these two linq queries of the same performance? And How to implement .Any in linq query?
- How to Implement this Sql block contain (Having , Join and Group By) with Linq
- How to bind and save an object containing a list of objects to database context in ASP.NET MVC with EF code first?
- How to dynamically create linq code at runtime and get results
- How to break link between source collection and LINQ result
- Entity Framework 4 and Linq to Entities specifications: How to code it?
- How to optimize a code using DataTable and Linq?
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- How to assign LINQ Query to a variable and then use it later in the code
- How to implement dynamic search functionality using C# and Linq?
- How to write SQL translateable linq code that groups by one property and returns distinct list
- LINQ query on dotnet code and how concat will work here
- How to implement search functionality when there is 3 texboxes and one search button in mvc 4
- how to implement join using LINQ and EntityFramework in MVC4
- How do I convert from entity/property names (conceptual) to table/column names (store)? (Trial code [in VB 2010 and EF 4] uses reflection.)
- How to insert a record in lambda expression and possible way to shorten the length of code
- How to perform .Max() on a property of all objects in a collection and return the object with maximum value
More Query from same tag
- How to use a simple select in entity framework 6
- LINQ to Entities: Lambda Expression Error
- linq query from one to many to many not letting me retrieve last many
- How to use LIKE Operator in LINQ to Entity Framework
- Using LINQ with Action to delete old files
- LINQ to XML problem with SharePoint Web Services
- linq join with case condition
- .Net Framework 4.5.1 RemoveAt
- Performance of FirstOrDefault()
- Append custom value to a LINQ statement
- Update like Sub query using Linq to Sql
- VS2010 linq Goes To shortcut
- Value was either too large or too small for an Int16 ,when copy to data table
- Building up a LINQ query based on bools
- Collections from LINQ to SQL and the ability to filter
- linq update not working
- How to to access ObjectContext objects while updating entities generated by ADO.NET
- Linq to sql, using like in dynamic where clause
- LINQ anonym object with result to delimited string (LINQ to Entities does not recognize the method 'System.String ToString()' method)
- List<object> select use multiple values
- Entity Framework: DefiningQuery error
- Is there a way to inline external functions into an EF Linq query?
- How can l use Regex.Replace method within LINQ
- IndexOutOfRangeException on Queryable.Single
- LINQ: How to get the Max Id with a group by clause?
- Linq - how to subtract current index DateTime item with previous index DateTime item
- How to search with LINQ entites having string[] array properties and find these containing any of string from string[] array?
- Group by string occurence using linq
- Getting a single ID from loop using LINQ
- How can I access the result of dynamic linq with dynamic key/value pairs of IEnumerable?