Reputation: 79
In this project there are two sql tables. The first table is Cars and the second is Mobiles. These two tables each have an ExpirationDate field. I want to show the last 10 fields from these two tables based on expiration date in one view.
public ActionResult Index(int ?page)
{
var result = db.Mercedes.Where(m => m.Username == User.Identity.Name).OrderByDescending(m=>m.CurrentDate);
var result2 = db.Mobiles.Where(m => m.UserName == User.Identity.Name).OrderByDescending(m => m.CurrentDate);
int pageSize = 6;
int pageNumber = (page ?? 1);
return View(result.ToPagedList(pageNumber, pageSize));
}
Upvotes: 1
Views: 63
Reputation: 7215
What you'll need to do is refactor those two queries to return the same class.
You then combine the two IQueryable sets and perform your Take(10) on the third, combined resultset.
example:
var result = from m in db.Mercedes
where m.Username == User.Identity.Name
orderby m.CurrentDate descending
select new car(){param1 = m.param1/*etc*/};
var result2 = from m in db.Mobiles
where m.Username == User.Identity.Name
orderby m.CurrentDate descending
select new car(){param1 = m.param1/*etc*/};
var combinelist = result.Concat(result2)
.OrderByDescending(m=>m.CurrentDate)
.Take(10)
.ToList();
Upvotes: 1
Reputation: 995
You can use the LINQ Concat and ToList methods:
var result = db.Mercedes.Where(m => m.Username == User.Identity.Name).OrderByDescending(m=>m.CurrentDate);
var result2 = db.Mobiles.Where(m => m.UserName == User.Identity.Name).OrderByDescending(m => m.CurrentDate);
var combinelist= result.Concat(result2).ToList();
Upvotes: 0