none
LINQ - Unrecognized expression node: ArrayIndex RRS feed

  • Question

  • I'm using a LINQ query that creates an anonymous type structure. The following query works fine:

    var q = from u in db.users
        join s in db.usr_settings on u.id equals s.user into x
        from s in x.DefaultIfEmpty()
        select new {
            u.id,
            u.name,
            user_group = ((s == null)? -1 : s.group_index)
        };

    But when I change the query to assign the user_group element as a string and populate it by indexing a static string array (see below)

    var q = from u in db.users
        join s in db.usr_settings on u.id equals s.user into x
        from s in x.DefaultIfEmpty()
        select new {
            u.id,
            u.name,
            user_group = ((s == null)? "" : GroupNames[s.group_index])
        };

    I get the following run-time error:

    Unrecognized expression node: ArrayIndex

    Does anyone have any ideas - am I doing something wrong or is it just not possible to do this in LINQ?

    Cheers

    Danny
    Monday, August 4, 2008 2:31 PM

Answers

  • You can always return the data from the server and then process the results explicitly on the client.

     

    var remoteQuery =

        from u in db.users
        join s in db.usr_settings on u.id equals s.user into x
        from s in x.DefaultIfEmpty()
        select new {
            u.id,
            u.name,

            groupIndex = (int?)s.group_index

        };

     

    var localQuery =

        from x in remoteQuery.AsEnumerable()

        select new {

             x.id,

             x.name,

             group = x.groupIndex == null ? "" : GroupNames[(int)x.groupIndex]

        };

     

     

     



     

    Tuesday, August 5, 2008 3:24 PM
    Moderator

All replies

  • LINQ to SQL does not know how to translate the array index operation into equivalent SQL.   It knows how to translate the comparison '==' and the ternary/conditional '?:' parts but not the indexer 'GroupNames[s.group_index]'. 

     

    Monday, August 4, 2008 3:56 PM
    Moderator
  • Does that mean that there is no way of doing what I'm trying to do apart from manually enumerating each option?

    e.g. Instead of
    "GroupNames[s.group_index]" to use:

    (s.group_index == 0)? GroupNames[0] : (s.group_index == 1)? GroupNames[1] : GroupNames[2];
    Monday, August 4, 2008 4:09 PM
  • You can always return the data from the server and then process the results explicitly on the client.

     

    var remoteQuery =

        from u in db.users
        join s in db.usr_settings on u.id equals s.user into x
        from s in x.DefaultIfEmpty()
        select new {
            u.id,
            u.name,

            groupIndex = (int?)s.group_index

        };

     

    var localQuery =

        from x in remoteQuery.AsEnumerable()

        select new {

             x.id,

             x.name,

             group = x.groupIndex == null ? "" : GroupNames[(int)x.groupIndex]

        };

     

     

     



     

    Tuesday, August 5, 2008 3:24 PM
    Moderator