To return only in a list you can use the method Concat.
Scenario 1:
I have two entities: courses
and packages
, whereas:
courses
has three fields (id, title and status) and;
packages
has two fields (id and title).
You need to normalize then by placing the same amount of fields in the entity packages
and also observe the field type so that there are no conversion errors. The field type has been set bool?
because the countryside status
of courses
is of that type; How to catch 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 take the same amount of entities' fields and they are the same, for example id and name, how it 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();
}
Observing: the two scenarios who solves is the SQL part of the database, with no low performance.
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);
}
Example Microsoft website:
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 the field types must be of the same type independent of the field name (although the alias can be made, for a good code reading) and in the same order.
if you query var query = from c in db.courses.Where(c => c.title.Contains(search)) select new { c }; it returns something?
– Marco Souza
Yes, returns when the search is done individually.
– Rafael Alexandre
Without the
Where
works?– Jéf Bueno
Searching individually yes, searching both tables at the same time no.
– Rafael Alexandre
And searching both tables without the Where clause?
– Jéf Bueno
Gives error: "A circular reference was detected when serializing an object of type 'System.Data.Entity.DynamicProxies.courses_F2EBA347335381B70B698E71131F0692220242D0763A30FAB52385A661639DFF'"
– Rafael Alexandre
Is there any way you can put the Models code? At least their properties.
– Jéf Bueno
Do you want each row of the Courses table to have a merged row of the Packages table? you have already checked how many rows each of them returns when done independently?
– Marco Souza
É Entity Framework?
– Leonel Sanches da Silva