3

How to do this inside in list (user.image = plugins.LoadImage(user.Image))
LoadImage is function which return image in the Form BitmapImage

 var query = (
                     from user in chatDBContext.tbl_User
                     select new
                         {
                         user.FirstName, user.LastName, user.Gender, user.Email,
                         user.DoB, user.Address, user.City, user.State,user.Country,
                         user.Quote, user.username, (user.image = plugins.LoadImage(user.Image))
                          }
                     ).ToList();
4
  • Is user.image = plugins.LoadImage(user.Image) not working for you? is it throwing any error/exception? Commented Feb 29, 2016 at 6:03
  • its give an error. An expression tree may not contain an assignment operator Commented Feb 29, 2016 at 6:08
  • It is Linq to Sql, this statement executes in the database. To fix this problem, move this Image conversion code outside. Just hold user.image = user.Image. Commented Feb 29, 2016 at 6:21
  • My bad, use image = user.Image Commented Feb 29, 2016 at 9:44

2 Answers 2

1

Consider that plugins.LoadImage(user.Image)) can't be executed in a linq2sql (nor EntityFramework) context.

So: execute the query first, take objects in memory (you can do that by call .ToList() or .ToArray() for example), then retrieve the image.

// database oriented query 
var query = from user in chatDBContext.tbl_User
            select user;

// take objects in memory THEN retrieve images
var objects = query.ToList().Select(user => new
{
    user.FirstName, user.LastName, user.Gender, user.Email,
    user.DoB, user.Address, user.City, user.State,
    user.Quote, user.username, image = plugins.LoadImage(user.Image)
});

Consider that you can avoid repeat all user fields simply selecting the entire object instead all single properties:

var objects = query.ToList().Select(user => new
{
    user, image = plugins.LoadImage(user.Image))
});

Usage:

foreach (var obj in objects)
{
    Console.WriteLine(obj.user.FirstName);
    Console.WriteLine(obj.user.LastName);
    // and so on all user properties

    Console.WriteLine(obj.image.Height); // just example
}

EDIT (clarification)

in your source code there is 2 distinct problems:

  1. You are trying to set the user.image property (i don't know if there is any property named image in your User class, anyway): you can't do an assignment into an anonymus object constructor. Instead, like my example or the other answer example, you should declare an image property right into the anonymus object initializer.

new { user.image = plugins.LoadImage(user.Image) }

should becomes

new { image = plugins.LoadImage(user.Image) }

  1. You can't execute all C# code into a linq2sql (nor EntityFramework), only few methods are allowed, like Convert.ToInt32(), .ToString(), etc.

For more info about the code that you can execute into a linq2sql query take a look here, otherwise for EntityFramework here

Sign up to request clarification or add additional context in comments.

Comments

1
var query = (from user in chatDBContext.tbl_User
                 select new
                     {
                     user.FirstName, user.LastName, user.Gender, user.Email,
                     user.DoB, user.Address, user.City, user.State,user.Country,
                     user.Quote, user.username, user.image = user.Image
                      }
                 ).ToList()
             .Select(c => new {
                     FirstName = c.FirstName, LastName = c.LastName, Gender = c.Gender, Email = c.Email,
                     DoB = c.DoB, Address = c.Address, City = c.City, State = c.State, Country = c.Country,
                     Quote = c.Quote, username = c.username, image = plugins.LoadImage(c.image)
                      }).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.