Reputation: 833
I am very new to mvc and trying to get the concept. I am following this instruction to deal with many many relationship. But I create an interface and entity framework and use ninject to bind them together. Instead of 'books'and 'users' (like in the tutorial), I use 'groups' and 'users'. I read many posts about the same error but they dont seem to solve my problem
public interface IGroups
{
IQueryable<Group> Groups { get; }
}
public class EFGroups : IGroups
{
private EFDbContextMainDb1 context = new EFDbContextMainDb1();
public IQueryable<Group> Groups
{
get;
set;
}
}
public class EFDbContextMainDb1 : DbContext
{
public DbSet<Group> tblGroup { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Group>().ToTable("tblGroup");
base.OnModelCreating(modelBuilder);
}
}
public class GroupListViewModel
{
public IEnumerable<GroupNetworkUser> GroupNetworkUsers { get; set; }
}
GroupListViewModel model = new GroupListViewModel
{
GroupNetworkUsers = testRepo.Groups.SelectMany(
group => group.Users,
(group, user) => new {
userName = user.vch_NetworkID
}
)
};
The error applies to "GroupNetworkUsers = testRepo.Groups.SelectMany(...". and says
Error 1 Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<Pho.Domain.Entities.GroupNetworkUser>'. An explicit conversion exists (are you missing a cast?) C:\visual_studio_project\pho\pho\Controllers\GroupsAndUsersController.cs 58 37 Pho.WebUI
Upvotes: 1
Views: 730
Reputation: 7126
You need to build a new GroupNetworkUser object. Currently you are building an anonymous object
GroupListViewModel model = new GroupListViewModel
{
GroupNetworkUsers = testRepo.Groups.SelectMany(
group => group.Users,
(group, user) => new GroupNetworkUser { // <-- Changed this line
UserName = user.vch_NetworkID
}
).ToList()
};
Upvotes: 1
Reputation: 9881
You need to convert it to a list:
GroupListViewModel model = new GroupListViewModel
{
GroupNetworkUsers = testRepo.Groups.SelectMany(
group => group.Users,
(group, user) => new {
userName = user.vch_NetworkID
}
).ToList()
};
Upvotes: 0