tags:

views:

192

answers:

2

I have a FormCollection and I just want to only iterate through the keys the do not contain the string pricing.

So what I tried was this...

foreach (var key in collection.AllKeys.Where(k => !k.Contains("Pricing"))){ ... }

The problem is the return is not a filtered list its returning boolean values... in which in need the filtered list of string...

AllKeys returns a string[] so in a sense I am just trying to filter a string[] here...

What I am missing here...

Thanks much!

+1  A: 

Here is the answer...

foreach (var key in collection.AllKeys.Where(k => !k.Contains("Pricing")).ToArray<string>()){ ... }
gmcalab
A: 

Are you sure that you're using Where and not Select?

Using Where will return an IEnumerable<string> which is what you're expecting.

Using Select will return an IEnumerable<bool> which is what you say is actually happening.

LukeH
Where does not Return the IEnumerable<string>I tried this...string[] list = collection.Where(o => !o.Contains("Pricing"));And it doesn't compile saying the return type is a bool.... I have figured out that answer and posted it.
gmcalab
@gmcalab: If `collection` is a `FormCollection`/`NameValueCollection` then your `Where` clause will definitely return `IEnumerable<string>`.
LukeH
+1 @Luke, Where will indeed return IEnumerable<string>foreach (string key in Request.Form.AllKeys.Where(k => !k.Contains("Pricing"))){}IEnumerable<string> collection = Request.Form.AllKeys.Where(k => !k.Contains("Pricing"));Both compile perfectly.
Mark
@Luke: Well, I thought it would return an IEnumerable<string> too, but it doesn't. It will not compile with the above syntax it says it returns a bool. It only worked when I added the .ToArray<string>()
gmcalab
@gmcalab: But you can only call `ToArray<string>` on an `IEnumerable<string>`. The return type of the `Where` call *must* be `IEnumerable<string>` or your `ToArray` call wouldn't compile either.
LukeH