Calling a method inside a Linq query

You have to execute your method call in Linq-to-Objects context, because on the database side that method call will not make sense – you can do this using AsEnumerable() – basically the rest of the query will then be evaluated as an in memory collection using Linq-to-Objects and you can use method calls as expected:

var q = (from a in v.A join b in v.B
        on a.i equals b.j
        where a.k == "aaa" && a.h == 0
        select new {T = a.i, Z = a.z })
        .Select(x => new { T = x.T, S = someMethod(x.Z).ToString() })

Leave a Comment