pass query with parameter to view - asp.net

Controller:
public ActionResult Univ(short id) {
var db = new DbEntities();
var query = from u in db.Universitates
join f in db.Facultates on u.IDUniv equals f.IDUniv
join s in db.Specializares on f.IDFac equals s.IDFac
where u.IDUniv == id
select new SearchViewModel
{
NumeUniv = u.NumeUniv,
OrasUniv = u.OrasUniv,
IDUniv = u.IDUniv,
NumeFac = f.NumeFac,
NumeSpec = s.NumeSpec
};
return View(query);
}
View:
#model Proj.Models.SearchViewModel
<h3>#Model.NumeUniv</h3>
I have the following error:
The model item passed into the dictionary is of type
'System.Data.Entity.Infrastructure.DbQuery`1[Proj.Models.SearchViewModel]',
but this dictionary requires a model item of type 'Proj.Models.SearchViewModel'.
Why is this?

The query is returning a enumerable list of SearchViewModel while your view is only looking for a single SearchViewModel.
Please try return View(query.First());

Related

Entity Framework 6 .add() with variable table and column name

I am able to get a somewhat dynamic Entity Framework search result with the following simplified example, which pulls a single result from the DB or Cache:
string strTableName = "TableName2"
string strColumnName = "MyColumnName"
int intPrimaryKey = 1
Type returnType;
returnType = typeof(TableName1);
string queryResults = null;
switch (strTableName)
{
case "TableName2":
returnType = typeof(TableName2);
break;
}
var refColumnName = returnType.GetProperty(strColumnName );
var query = mydbEntity.Set(returnType).Find(intPrimaryKey );
var queryResults = refColumnName.GetValue(query).ToString();
This can also be adapted for Updating a record:
DataQuery.LeadsEntity.Entry(query).Property(strColumnName ).CurrentValue = "Whatever";
DataQuery.LeadsEntity.SaveChanges();
Is there an equivalent for way for .set(returnType).Add()? I'm not sure if there is a way to do this type of thinking using variable table and column names:
DataQuery.LeadsEntity.Set(returnType).Add(new returnType { PrimayKeyName = 1, refColumnName = "Something" });
If you don't know a priori what's the name of the primary key property, it could be a little painful to get it from the type.
This is the way I found more reliable to retrieve the primary key from the entity type:
private string[] GetKeyNames(DbContext context, Type entityType)
{
ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
//create method CreateObjectSet with the generic parameter of the base-type
MethodInfo method = typeof(ObjectContext).GetMethod("CreateObjectSet", Type.EmptyTypes)
.MakeGenericMethod(entityType);
dynamic objectSet = method.Invoke(objectContext, null);
IEnumerable<dynamic> keyMembers = objectSet.EntitySet.ElementType.KeyMembers;
string[] keyNames = keyMembers.Select(k => (string)k.Name).ToArray();
_keyNamesCache[entityType] = keyNames;
return keyNames;
}
But assuming your primary keys are always a single property, you could use reflection to create the entity object and set its properties like this:
private void CreateEntity(Type entityType, object pkValue, Dictionary<string, object> Columns)
{
// Create the new entity
var entity = Activator.CreateInstance(entityType);
// Get the primary key property name
var pkName = GetKeyNames(context, entityType).First();
// Set Pk value
entityType.GetProperty(pkName).SetValue(entity, pkValue);
// Set other column(s)
foreach (var col in Columns)
{
entityType.GetProperty(col.Key).SetValue(entity, col.Value);
}
// Add the entity to the DbSet
using (var context = new YourContext())
{
context.Set(entityType).Add(entity);
}
}

Asp Core, How to use PagingList<T>.CreateAsync() with a viewModel?

I am working on a asp.net core 1.1 project and i want to create paging in my some views. I studied microsoft documents about paging in asp core but it is very simple mode with 1 table. In my view i use multi table and use a viewmodel to initialize it. I want to use PagingList<T>.CreateAsync() method to create paging but get error:
can not convert from system.linq.Iqueryable<> to system.linq.IorderedQueryable<>
my action:
[HttpGet]
public async Task<IActionResult> Index(int? page)
{
List<BookListViewModel> model = new List<BookListViewModel>();
var query = (from b in _context.books
join a in _context.authors on b.AuthorID equals a.AuthorId
join bg in _context.bookgroups on b.BookGroupID equals bg.BookGroupId
select new
{
b.BookId,
b.BookName,
b.BookPageCount,
b.BookImage,
b.AuthorID,
b.BookGroupID,
a.AuthorName,
bg.BookGroupName
});
foreach (var item in query)
{
BookListViewModel objmodel = new BookListViewModel();
objmodel.BookId = item.BookId;
objmodel.BookName = item.BookName;
objmodel.BookImage = item.BookImage;
objmodel.BookPageCount = item.BookPageCount;
objmodel.AuthorId = item.AuthorID;
objmodel.BookGroupId = item.BookGroupID;
objmodel.AuthorName = item.AuthorName;
objmodel.BookGroupName = item.BookGroupName;
model.Add(objmodel);
}
ViewBag.RootPath = "/upload/thumbnailimage/";
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(await PagingList<BookListViewModel>.CreateAsync(model.AsQueryable() , pageNumber, pageSize));
}
I have not yet written anything about paging in index view and it is a simple list of viewmodel
Well can't really be sure from the code you posted. But the exception says the the CreateAsync method needs IOrderedQueryable, but you're giving it an IQueryable.
Try changing it to pass in the query object (which I guess should implement the IOrderedQueryable, if you're using Entity framework).
The idea behind the PagingList (presumably) is to use it to do the paging in the database.
What you're doing is bringing the filterted set into memory (when for-eaching through the result), and then doing doing the paging.
The code might look something like this:
[HttpGet]
public async Task<IActionResult> Index(int page = 1)
{
var query = (from b in _context.books
join a in _context.authors on b.AuthorID equals a.AuthorId
join bg in _context.bookgroups on b.BookGroupID equals bg.BookGroupId
select new BookListViewModel()
{
BookId = b.BookId,
BookName = b.BookName,
BookPageCount = b.BookPageCount,
BookImage = b.BookImage,
AuthorId = b.AuthorID,
BookGroupId = b.BookGroupID,
AuthorName = a.AuthorName,
BookGroupName = bg.BookGroupName
}).AsNoTracking().OrderBy(u => u.BookId);
ViewBag.RootPath = "/upload/thumbnailimage/";
var pagedResult = await PagingList<BookListViewModel>.CreateAsync(query, 10, page);
return View(pagedResult);
}
Hope it helps.

LINQ dynamic property in select

// Hi everyone
i do this call in Action :
[HttpGet]
public virtual ActionResult JsonGetProvinces(int countryId)
{
//WebSiteContext WbContext = new WebSiteContext();
//UnitOfWork UnitofWork = new UnitOfWork(WbContext);
var provinces =
(
from province in unitofWork.ProvinceRepository.All
where province.CountryId == countryId
select new
{
Id = province.Id,
Name = province.GetType().GetProperty("Name_" + CultureManager.GetCurrentCultureShortName()).GetValue(province)
}
).ToList();
return Json(provinces, JsonRequestBehavior.AllowGet);
}
something is wrong with my query :
var provinces =
(
from province in unitofWork.ProvinceRepository.All
where province.CountryId == countryId
select new
{
Id = province.Id,
Name = province.GetType().GetProperty("Name_" + CultureManager.GetCurrentCultureShortName()).GetValue(province)
}
).ToList();
Particulary,
Name = province.GetType().GetProperty("Name_" + CultureManager.GetCurrentCultureShortName()).GetValue(province)
In BDD, there is Name_fr, Name_en columns
and i'm trying to take one dynamically... Is it possible ?
Of course, i can take both and choose dynamically the column in View but i would to know how do...
Thank you for your help
The short answer is you need to change your code a bit and using expression tree inside. Look at this question
EF can not translate function calls to SQL. Using expression trees can be comlicated see this question
Here is a sample with expression trees. The GetQuery2 is the same as GetQuery but with expression tree and a propertyname parameter.
public static IQueryable<Foo> GetQuery(BlogContext context)
{
var query = from x in context.BlogEntries
select new Foo
{
NameX = x.Name
};
return query;
}
public static IQueryable<Foo> GetQuery2(BlogContext context, string propertyName)
{
ConstructorInfo ci = typeof(Foo).GetConstructor(new Type[0]);
MethodInfo miFooGetName = typeof(Foo).GetMethod("set_NameX");
MethodInfo miBlogEntry = typeof(BlogEntry).GetMethod("get_" + propertyName);
ParameterExpression param = Expression.Parameter(typeof(BlogEntry), "x");
IQueryable<Foo> result = Queryable.Select<BlogEntry, Foo>(
context.BlogEntries,
Expression.Lambda<Func<BlogEntry, Foo>>(
Expression.MemberInit(
Expression.New(ci, new Expression[0]),
new MemberBinding[]{
Expression.Bind(miFooGetName,
Expression.Property(param,
miBlogEntry))}
),
param
)
);
return result;
}
It is easier the fetch all all language strings and write an additional Property Name that does the magic.

How to bind an entity framework object to a listview

This is the first time to ask here, so please help me convey what I mean.
First I used an entity framework and I want to make a query that retrieve data from 3 tables, and then bind the result to a list view, the problem here is that there are nothing appear on the page. That's what I made:
ITIEntities MyContext = new ITIEntities();
var Courses = MyContext.Courses;
var Instructors = MyContext.Instructors;
var Ins_Courses = MyContext.Ins_Course;
var Query= from Crs in Courses
from Ints in Instructors
from Instructor_courses in Ins_Courses
where Crs.Crs_Id==Instructor_courses.Crs_Id
&& Instructor_courses.Ins_Id== Ints.Ins_Id
select new
{
Name= Ints.Ins_Name,
Salary=Ints.Salary,
ListOfCourses=Crs.Crs_Name,
};
MyListView.DataSource = Query;
MyListView.DataBind();
MyContext.SaveChanges();
here problem is enumerable type variable Query so if you want to bind result to listview you have to define entity public class with getter and setter properties for each column which you want to show.
public Class ClassNameEntity
{
public string Name {get; set;}
}
use in linq query with following
select new ClassNameEntity
{
Name= Ints.Ins_Name
}.ToList();
that will return list of your defined class object list so directly bind result of upper query.
ITIEntities MyContext = new ITIEntities();
var Courses = MyContext.Courses;
var Instructors = MyContext.Instructors;
var Ins_Courses = MyContext.Ins_Course;
List<ClassNameEntity> Query= from Crs in Courses
from Ints in Instructors
from Instructor_courses in Ins_Courses
where Crs.Crs_Id==Instructor_courses.Crs_Id
&& Instructor_courses.Ins_Id== Ints.Ins_Id
select new ClassNameEntity
{
Name= Ints.Ins_Name,
Salary=Ints.Salary,
ListOfCourses=Crs.Crs_Name,
}.ToList();
MyListView.DataSource = Query;
MyListView.DataBind();
MyContext.SaveChanges();

Not able to return JsonResult

The following query is working successfully.
var tabs = (
from r in db.TabMasters
orderby r.colID
select new { r.colID, r.FirstName, r.LastName })
.Skip(rows * (page - 1)).Take(rows);
Now I want to return JsonResult as like
var jsonData = new
{
total = (int)Math.Ceiling((float)totalRecords / (float)rows),
page = page,
records = totalRecords,
rows = (from r in tabs
select new { id = r.colID, cell = new string[] { r.FirstName, r.LastName } }).ToArray()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
But it will gives me an error like:
The array type 'System.String[]' cannot be initialized in a query result. Consider using 'System.Collections.Generic.List`1[System.String]' instead.
What should I do to get expected result?
I suspect that it's as simple as pushing the last part into an in-process query using AsEnumerable():
var jsonData = new
{
total = (int)Math.Ceiling((float)totalRecords / (float)rows),
page = page,
records = totalRecords,
rows = (from r in tabs.AsEnumerable()
select new { id = r.colID,
cell = new[] { r.FirstName, r.LastName } }
).ToArray()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
You may want to pull that query out of the anonymous type initializer, for clarity:
var rows = tabs.AsEnumerable()
.Select(r => new { id = r.colID,
cell = new[] { r.FirstName, r.LastName })
.ToArray();
var jsonData = new {
total = (int)Math.Ceiling((float)totalRecords / (float)rows),
page,
records = totalRecords,
rows
};
It's because it's adding to the LINQ query that is your tabs IQueryable. That is then trying to turn the LINQ expression into a SQL query and the provider doesn't support projecting arrays.
You can either change the assignment of the tabs variable's LINQ expression to use ToList to materialize the DB results right then and there, or you can add .AsEnumerable() to the LINQ expression assigned to the rows field of the anonymous type that is your JsonResult. AsEnumerable will demote the IQueryable to an IEnumerable which will prevent your second LINQ query from trying to be added to the DB query and just make it a LINQ-to-objects call like it needs to be.

Resources