tags:

views:

43

answers:

1

Hi all,

I am quite sure this question has already been asked several times and I do apolgize for asking it once again, but I made a few research and unfortunately I did not find my pleasure on the internet...

I have a IQueryable like this :

triggers = triggers.Where(t => GetIdFromName(t.Name) == filter.Id.ToString());

The function GetIdFromName get a part of the name to retrieve the Id :

public string GetIdProfiloFromName(string name)
        {
            return name.Substring(name.IndexOf(NameFirstSeparator)+1,name.IndexOf(NameSecondSeparator)-name.IndexOf(NameFirstSeparator)-1);
        }

I know it is not nice, but it is the best I could managed to do so far. My question is that using Linq to sql is not permitted using these two statements. The application throws an error, whereas this is ok :

triggers = triggers.Where(t => t.Name.Substring(t.Name.IndexOf(NameFirstSeparator) + 1, t.Name.IndexOf(NameSecondSeparator) - t.Name.IndexOf(NameFirstSeparator) - 1) == filter.Id.ToString());

I suspect that the function GetIdFromName should give something different than a string, but I really wonder what and how...

Thanks for your enlightenment,

Yoann

[EDIT] Update to what I understood so far:

I cannot do what i wanted, because in order to do so I would need to do something of this kind :

static Expression<Func<Trigger,string,  bool>> IsId = (a,b) => a.name.Substring(a.name.IndexOf(NameFirstSeparator) + 1, a.name.IndexOf(NameSecondSeparator) - a.name.IndexOf(NameFirstSeparator) - 1)==b;

But this would not get into

triggers = triggers.Where(func<Trigger,bool>);

And I could manage to do it, but I would have to get all the results from my database to perform my test in memory.

Thanks a lot all, you made this get really clear to me!!

+3  A: 

LINQ 2 SQL is converting your query into an expression tree, and then translating that expression tree into SQL. Since your custom function doesn't have an SQL correlated function, it doesn't know how to translate it and throws an exception.

David Pfeffer
Thanks for your quick answer. I supected that, but the real things would be to know how to externalize these bits of code to "external" functions. This substring stuff here is pretty ugly and heavy and I would have liked to encapsulate it in a nice function. That should be possible because linq to sql is able to understand the raw format of it.
Yoann
Unfortunately you can't do what you're looking for. Expression trees only support a single expression, represented as a tree. To support methods in the manner you want, it would have to be able to handle blocks. For example, `x => { return x; }` cannot be compiled into an expression tree.
David Pfeffer
+1 For a good analysis.
KMan
You can get around this limitation by calling `AsEnumerable()` before using your method. However, this has a big downside in that everything before the AsEnumerable call gets pulled into memory. Post `AsEnumerable()`, the query is LINQ to Objects with all the power it provides.
Matt Greer
Thanks David...
Yoann