Darren
Darren

Reputation: 11021

Selecting specific columns as certain names in LINQ?

I am trying to populate my WebGrid from LINQ to SQL.

It has the following columns: Username Email First Name Last Name IsApproved IsLockedOut IsOnline LastActivityDate LastLoginDate CreationDate

All of this comes from the User object: dbc.Users

However everything except First Name and Last Name is an association from aspnet_Membership and aspnet_User which is defined in my LINQ to SQL. How do I set these as the proper column names?

I tried:

var accounts = dbc.Users.Select(User => new User
{
      Username => aspnet_Membership.username
}).ToList();

For username but it is not working.

How would I do this?

Upvotes: 0

Views: 1631

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

It would be:

var accounts = dbc.Users.Select(User => new User
{
      Username = aspnet_Membership.username
}).ToList();

No => for property assignment, but assuming that's not the case and what you need is to "flatten" the resultset between multiple objects, one way to work around having one entity with multiple tables is to manage this by using an anonymous class:

var accounts = from u dbc.Users
               let m = dbc.aspnet_Memberships.First(i => i.ProviderUserKey == u.UserKey)
               select new
    {
          Username = m.username,
          FirstName = u.FirstName
    }).ToList();

Or, define a view, and add this to your model. This is another way to have a common entity.

Upvotes: 2

Related Questions