I want to return result 3 to the main method
var result = (from od in orders
join em in employees on od.EmployeeID equals em.EmployeeID
join ct in customers on od.CustomerID equals ct.CustomerID
//orderby em.EmployeeID
select new
{
od.OrderID,
od.ShipCountry,
ct.CompanyName,
ct.ContactName,
FullName = em.FirstName + ' '+ em.LastName,
});
var newOrders = result.OrderBy("OrderID DESC");
var result3 = newOrders
.ToList()
.Skip(rowsPerPage * (page-1))
.Take(rowsPerPage);
return result3;
}
public class MyJoin {
public int OrderID { get; set; }
public DateTime OrderDate { get; set; }
public string ShipCountry { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string EmployeeName { get; set; }
}
I'm not sure how to return it as a simple
return result3;
Does not do the trick, I get an error
Cannot implicitly convert type
System.Collections.Generic.List<AnonymousType#1>
toSystem.Collections.Generic.List<UserQuery.MyJoin>
It is not working because in here:
select new
{
od.OrderID,
od.ShipCountry,
ct.CompanyName,
ct.ContactName,
FullName = em.FirstName + ' '+ em.LastName,
}
You are creating new type -> AnonymouseType, and it is not MyJoin
type which you are expecting to be returned in your function. Simply use:
select new UserQuery.MyJoin
{
OrderID = od.OrderID,
...
}
of course you need to fill fields from MyJoin
class.
You cannot return an anonymous type from a function call.
You will have to explicitly define the type you expect anyhow, then
var result = (from od in orders
join em in employees on od.EmployeeID equals em.EmployeeID
join ct in customers on od.CustomerID equals ct.CustomerID
//orderby em.EmployeeID
select new UserQuery.MyJoin
{
od.OrderID,
od.ShipCountry,
ct.CompanyName,
ct.ContactName,
FullName = em.FirstName + ' '+ em.LastName,
});
var newOrders = result.OrderBy("OrderID DESC");
var result3 = newOrders
.Skip(rowsPerPage * (page-1))
.Take(rowsPerPage);
.ToList()
return result3;
Note that you do not need to materialize the result (no .ToList() needed) if this is Linq to Objects, or if the DbContext is available to the caller.
Specify the type -
In your case
IQueryable<UserQuery.MyJoin> result = (from od in orders
join em in employees on od.EmployeeID equals em.EmployeeID
join ct in customers on od.CustomerID equals ct.CustomerID
//orderby em.EmployeeID
select new UserQuery.MyJoin
{
od.OrderID,
od.ShipCountry,
ct.CompanyName,
ct.ContactName,
FullName = em.FirstName + ' '+ em.LastName,
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.