views:

37

answers:

2

I'm using a query that tries to find a matching unique identifier (guid) such as

var myUsers = from u in table
              where u.UniqueIdentifierId == MyClass.GetCurrentUserId()
              select u;

This throws the Method Not Supported error for my custom function. I recoded it to do this

string guid = MyClass.GetCurrentUserId().ToString();

where u.UniqueIdentifierId.ToString() == guid

And this works. I want to know if there are any other work arounds that do not require creating a temporary variable. Thanks.

+3  A: 

You can't call it in the query, because LINQ to SQL can't translate this. It tries to transform this in a SQL call. What you must do is call this method outside of the LINQ query, as follows:

var guid = MyClass.GetCurrentUserId();

var myUsers = 
    from u in table
    where u.UniqueIdentifierId == guid
    select u;
Steven
@Steven This is correct but it still requires the temporary variable. I am looking for any alternatives. Apparently there are no other solutions?
Curtis White
+1  A: 

Did you try?

var myUsers = from u in table
          where u.UniqueIdentifierId == MyClass.GetCurrentUserId().ToString()
          select u;

This would force LINQ to SQL to use string comparison(which is why the second one worked)

Nix
@Nix No that version doesn't work.
Curtis White
@Nix To be clear, it is the method call -- not the string comparison that is the issue.
Curtis White