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:
- 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) }
- 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
user.image = plugins.LoadImage(user.Image)not working for you? is it throwing any error/exception?Linq to Sql, this statement executes in the database. To fix this problem, move this Image conversion code outside. Just holduser.image = user.Image.image = user.Image