tags:

views:

145

answers:

4

I want to select a token out of a string if it exists in the string, I've got as far the following but I'm unsure why it doesn't compile:

IList<string> tokens = _animals.Split(';');

Func<string, bool> f1 = str => str.Contains("Dog");
Func<string, Func<string, bool>, string> f2 = str => Equals(f1, true);

var selected = tokens.Select(f2);

Cheers

Ollie

+3  A: 

Do you really need LINQ to do that? Why can you do something like this:

_animals.Contains("Dog")
Andrew Hare
No need to make your problem more complicated than it ought to be. I suggest finding an example more suited to Linq. Create a list of strings with the names of Presidents (or whoever) and then write a Linq query to pull out all the names starting with "L" or "Wa" or whatever.
Terry Donaghe
Not sure this is quite what the asker wants, but I could be wrong.
Noldorin
Its kind of silly to select out a token that you are passing in to see if it exists. If the string contains the token, why do you need to select it out again?
Andrew Hare
It's not quite the same. The tokens themselves might be delimited strings - (Cat:Tom;Mouse:Jerry;Dog:Rover;Dog:Fido;). The linq code when then allow him to extract (project) just the names.
Joel Coehoorn
Joel's on the money...
AWC
+5  A: 

I think you just want this.

var selected = tokens.Where(str => str.Contains("Dog"));
Noldorin
A: 

Or in words

var selected = from token in tokens where token.Contains("Dog") select token;
Dario
Yeah, that does the job too, though I the query syntax is more than what's necessary in this case (a simple call to the Where extension method will do).
Noldorin
@Noldorin this version is the comprehension syntax version of your solution
Lucas B
A: 

try this

    var result = (from p in tokens where p.Contains("Dog") select p);
Fusspawn