Reputation: 37
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>
Upvotes: 0
Views: 102
Reputation: 150148
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.
Upvotes: 0
Reputation: 23107
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.
Upvotes: 4
Reputation: 69
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,
});
Upvotes: 0