I am having trouble with the Select
method in my repository method when I try to use certain return types.
The repository method where I am having the issue is:
public IEnumerable<T> List(Expression<Func<T, bool>> filter = null,
string include = "",
int Taked = 0, Expression<Func<T, T>> selector = null)
{
IQueryable<T> query = dbSet;
if (filter != null)
query = query.Where(filter);
#region Stringleri İnclude Eder
foreach (var includeProperty in
include.Split(new char[] {','},
StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
#endregion
if (selector != null)
query = query.Select(selector);
if (Taked != 0)
return query.Take(Taked).ToList();
return query.ToList();
}
internal DbContext context;
internal DbSet<T> dbSet;
I want to return my entity class using the method above, but I only want certain properties to be populated. I have tried the following approach:
AdminWork workunit = new AdminWork();
IEnumerable<AdminMenu> adminMenus = workunit.Menu.List(x => x.Online == true,
selector: z => new AdminMenu
{
MenuID = z.MenuID,
Name = z.Name,
Path = z.Path
});
Which throws the exception:
AdminMenu cannot be constructed in a LINQ to Entities query
I have also tried the following approach but it requires to return an IEnumerable<int>
:
IEnumerable<AdminMenu> menus = workunit.Menu.List(x => x.Online == true,
selector: z => z.MenuID);
My question is how can I create new instances of my entity class in linq to entities, so not every property is populated.