views:

70

answers:

3

I'm having a problem when I attempt to create a generic List using a POCO within a LINQ query that creates another POCO:

var user =
from u in ctx.users
select new User
{
    id = u.UserID,
    name = u.Name,
    roles = u.Roles.ToList()
};

I have created both a User and Role class. If I define 'roles' in User as:

public List<Roles> roles;

I have no problem. This is using the Entities Roles object however, and I'd rather use my own Role POCO:

public List<Role> roles;

However this throws the following error:

Error 3 Cannot convert lambda expression to type 'string' because it is not a delegate type

How exactly do I use my POCO rather than the Entity object?

A: 

Do you have a namespace:

 using System.Linq;
Nix
+2  A: 
var user =
from u in ctx.users
select new User
{
    id = u.UserID,
    name = u.Name,
    roles = from r in u.Roles
            select new Role 
            { 
                ...
            }
};
Craig Stuntz
This doesn't quite work, was wondering if you could shed some light: as-is, that code generates the same exception as I had before. If I wrap "from" to the closing "}" in (), I am able to call .ToList() and the code will compile. However, it generates a new exception: "LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[MVC1.Models.Role] ToList[Program](System.Collections.Generic.IEnumerable`1[MVC1.Models.Role])' method, and this method cannot be translated into a store expression."(I'm using 3.5, if it's relevant.)
lynx_guy
The `User.Roles` property cannot be a string. It needs to be a list of some kind (e.g., `IList<string>`).
Craig Stuntz
A: 
List<Data.User> userEntities = ctx.Users.Include("Roles").ToList();

List<POCO.User> users =
  from u in userEntities
  select new User()
  {
    id = u.UserId
    name = u.Name
    roles =
    (
      from r in Roles
      select new Role()
      {

      }
    ).ToList()
  }
David B