1

Here is my query

return (from l in Context.DctLink
        join t in Context.DctTabel on l.BType equals t.DocType
        join t2 in Context.DctTabel on l.TabelNr equals t2.TabelNr
        where l.AType == docType || l.AType == 0
        select new { t.TabelNr, t2.Naam, t.Titel })
     .Union(from l in Context.DctLink
        join t in Context.DctTabel on l.AType equals t.DocType
        join t2 in Context.DctTabel on l.TabelNr equals t2.TabelNr
        where l.BType == docType || l.BType == 0
        select new { t2.TabelNr, t2.Naam, t.Titel })
     .Join(Context.TimIcon.Where(q => q.Timweb && q.ShowId.ToInt32() > 0),
        x => x.TabelNr,
        y => y.TabelNr,
        (x, y) => new LookupItem
        {
           Id = x.TabelNr,
           Name = x.Titel,
           Tag = x.Naam
        }).ToList();

I want to be able to do this q.ShowId.ToInt32() > 0. But I get a System.Unsupported Exception. Isn't this possible in a link query or am I just overlooking something simple

Thanks in advance

Sandman
  • 94
  • 9
  • 1
    Of what type is `q.ShowId`? –  Jan 20 '15 at 13:42
  • q.ShowId is a byte[] @pwas – Sandman Jan 20 '15 at 13:45
  • What with `q.ShowId != null` ? Heve you checked http://stackoverflow.com/questions/17038334/ef5-fluent-api-byte-array-to-long ? – rraszewski Jan 20 '15 at 13:46
  • @Sandman how it is converted to int?: I mean what is behind the scenes? –  Jan 20 '15 at 13:47
  • What is the SQL you want to convert it to? You could use `EdmFunctionAttribute` to add a method to EF. Although it sounds like you just want to do `(int) q.ShowId`. – Aron Jan 20 '15 at 13:48
  • possible duplicate of [Entity Framework/Linq EXpression converting from string to int](http://stackoverflow.com/questions/16694716/entity-framework-linq-expression-converting-from-string-to-int) – jessehouwing Jan 20 '15 at 14:27
  • See the second answer on the quoted question for all the options available to you. Basically you need to explain EntityFramework how to map the Function ToInt32 to SQL code. – jessehouwing Jan 20 '15 at 14:28

2 Answers2

0

It depends on your LINQ provider. LINQ to Objects supports pretty much anything. The one you're using (LINQ to Entities or LINQ to SQL or something similar) doesn't support everything, because it needs to understand your expression and translate it to SQL (it can't do that with any kind of expression).

The simplest way to fix this is to call AsEnumerable() at some point, in order to convert the sequence (up to that point) to an in-memory sequence, so you'll fall back to LINQ to Objects and you can perform the (previously unsupported) logic on it.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
0

You need to fetch the data from db using AsEnumerable or ToList, then you can use any method you want. Otherwise it's not possible because EF Query Provider can't know how to translate your method into SQL.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • Although this is correct, it's worth mentioning that `AsEnumerable` or `ToList` should be called as "late" as possible in the query - i.e. as much of the data processing should be done as possible on the DB before the results are returned to the caller for further processing (at least IMO). – Daniel Kelley Jan 20 '15 at 13:57