tags:

views:

52

answers:

2

I am learning LINQ, and I am not sure how to write a query to return a boolean indicating whether an item is found in an array. I have a very simple list:

var targetProperties = new string[] { "SelectedDate", "SelectedMonth" };

I need to write a LINQ query that will return true if an item passed in is in the array, and false if it isn't. What would that query look like? Thanks for your help.

+8  A: 

targetProperties.Contains("SelectedDate") ?

jdv
Arrays don't have a `Contains` method. You need to use the static Array.IndexOf method. http://msdn.microsoft.com/en-us/library/system.array.indexof.aspx
dtb
It is a linq extension method on `IEnumerable<T>`, which is implemented on C# arrays.
jdv
8 upvotes... 2 more and I'm a populist... http://stackoverflow.com/badges/62/populist
jdv
+4  A: 
bool answer = targetProperties.Any(x => x == "SelectedDate");
Cylon Cat