To return only to a list you can use the concat .
Scenario 1:
I have two entities: courses
and packages
, where:
-
courses
has three fields ( id, title and status ) and;
-
packages
has two fields ( id and title ).
You then need to normalize by putting the same amount of fields in the packages
entity and also observe the field type so that it has no conversion errors. The type bool?
has been set because the status
field of courses
is of this type; How to get the field type: é só olhar na entidade mapeado o seu tipo
using (MyDbContext db = new MyDbContext())
{
var courses = db.Cours.Select(c => new
{
c.Id,
c.Title,
c.Status
});
var packages = db.Packages.Select(c => new
{
c.Id,
c.Title,
Status = new bool?()
});
var js = courses.Concat(packages);
var items = js.ToList();
}
Scenario 2:
I want to get the same both entities fields and they are equal, for example id and name , as would be:
using (MyDbContext db = new MyDbContext())
{
var courses = db.Cours.Select(c => new
{
c.Id,
c.Title
});
var packages = db.Packages.Select(c => new
{
c.Id,
c.Title
});
var js = courses.Concat(packages);
var items = js.ToList();
}
Note: The two scenarios that resolve is the SQL part of the database, and there is no downtime.
In your code :
[HttpGet]
public JsonResult Autocomplete(Entities db, string search)
{
JavaScriptSerializer json = new JavaScriptSerializer();
var courses = db.Courses.Select(c => new
{
c.Id,
c.Title
});
var packages = db.Packages.Select(c => new
{
c.Id,
c.Title
});
var js = courses.Concat(packages);
return Json(js.Where(c => c.Title
.Contains(search))
.ToList(), JsonRequestBehavior.AllowGet);
}
Microsoft Site Example:
Concatenate Two Sequences
IQueryable<String> custQuery =
(from cust in db.Customers
select cust.Phone)
.Concat
(from cust in db.Customers
select cust.Fax)
.Concat
(from emp in db.Employees
select emp.HomePhone);
foreach (var custData in custQuery)
{
Console.WriteLine(custData);
}
This example illustrates very well that field types must be of the same type regardless of the field name (although aliasing can be done for good code reading) and in the same order. >