Quick LINQ question

I'm throwing this out there hoping there's a LINQ guru lurking among my readership somewhere.

I'm trying to add a Row Number Column to a LINQ query. In fact, this looks to be exactly what I want to do, however his example won't compile for me - "An expression tree may not contain an assignment operator".

I'm hoping there's something simple and obvious I've missed. My code looks something like:

int i = 1;

args.Result = from userimage in db.UserImages
              where userimage.UserID == userGuid.Value
              select new
              {
                  userimage.ImageID,
                  Name = userimage.Image.ImageName,
                  rowId = i++
              };

ImageID is a Guid in this case, otherwise I could probably make do with that.

Tags:
Posted on Monday, June 09, 2008 11:25 PM |

Like this? Share it!

  • # re: Quick LINQ question
    Gravatar
    Commented on 6/12/2008 2:38 AM

    Tried it out and the basic syntax seems fine.

    It could be the args.Result =, as it depends on what type "Result".
    You might new a DefaultOrSingle to only return one object to "Result".

  • # re: Quick LINQ question
    Gravatar
    Commented on 6/12/2008 8:41 AM

    Cheers for testing that out :)

    The code is running inside the Selecting event of a LINQDataSource, so it's definately expecting multiple results - plus the code runs fine returning multiple results if I take the attempted rowId code out. I also tried not using the args.Result, and just throwing the results into a var and it still doesn't compile.

    Hmm although as I type this I have another idea.. investigation required ;)

Post a comment
Please add 2 and 7 and type the answer here:
Remember me?
Ensure the word in this box says 'orange':