我知道這已經在之前討論過了,我一直在仔細閱讀並嘗試所有這些,但每個解決方案都會返回一個List,而我明確地需要返回IQueryable才能爲已經實現的異步分頁方法提供資源。實體或複雜類型'xyz'不能在LINQ to Entities查詢中構建
ASP NET MVC5 Web應用程序,EF6時,Visual Studio 2015年
public partial class Product
{
public int? ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int CategoryID { get; set; }
public virtual Category Category { get; set; }
public virtual CategoryTrans CategoryTrans { get; set; }
}
public partial class Category
{
public int ID { get; set; }
public string Name { get; set; }
public int? ParentID { get; set; }
public bool IsDeleted { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<CategoryTrans> CategoryTrans { get; set; }
}
public class ISO_Languages
{
public int ID { get; set; }
public string code { get; set; }
public bool IsEnabled { get; set; }
public string name_en { get; set; }
public string name_fr { get; set; }
public string name_it { get; set; }
public string name_de { get; set; }
public string name_es { get; set; }
}
public class CategoryTrans
{
[Key, Column(Order = 1)]
public int category_id { get; set; }
[Key, Column(Order = 2)]
public int language_id { get; set; }
[ForeignKey("category_id")]
public virtual Category categoryId { get; set; }
[ForeignKey("language_id")]
public virtual ISO_Languages languageId { get; set; }
public string name { get; set; }
}
下面的查詢返回p.Category.CategoryTrans ALL CategoryTrans,這意味着任何類別的轉換(這是一個起點雖然,不是我需要的):
public static IQueryable<Product> ActiveProductsPerUser(BaseContext db, string userid, string culture)
{
var query = (from p in db.Products
join ct in db.CategoryTrans
on p.CategoryID equals ct.category_id
join l in db.ISO_Languages
on ct.language_id equals l.ID
where l.code.Substring(0, 2) == culture
select p);
return query;
}
我想要做的是過濾,對於每個產品,單個類別translatio n,取決於文化輸入參數。喜歡的東西:
public static IQueryable<Product> ActiveProductsPerUser(BaseContext db, string userid, string culture)
{
var query = from p in db.Products
join ct in db.CategoryTrans
on p.CategoryID equals ct.category_id
join l in db.ISO_Languages
on ct.language_id equals l.ID
where l.code.Substring(0, 2) == culture
select new Product
{
ID = p.ID,
Name = p.Name,
Description = p.Description,
CategoryTrans = p.Category.CategoryTrans.Where(b => b.language_id.Equals(l.ID)).SingleOrDefault()
};
return query;
}
但返回錯誤:
The entity or complex type 'xyz.DAL.Product' cannot be constructed in a LINQ to Entities query.
我嘗試了好幾種方案,但沒有奏效。我的理解是返回LINQ到實體,而不是LINQ到對象,但實體需要什麼,我肯定需要一個產品的IQueryable返回負責下面的異步方法用於尋呼&排序問題:
public async Task<ActionResult> UserList(int? page, int sortBy = 1)
{
int currentPage = (page ?? 1);
ViewBag.CurrentPage = currentPage;
viewModel.productList = await userlist(sortBy).ReturnPages(currentPage, Constants.PageItems);
return View(viewModel);}
在等待「用戶列表」輸入是Product IQueryable,由我努力實現的方法返回。
https://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities-query – levent
levent,感謝您閱讀我的文章更仔細。我閱讀這篇文章,並建議解決方案返回一個列表,這不是我所需要的。 – Luke
ok sory ..'l.code.Substring(0,2)== culture'和'b => b.language_id.Equals(l.ID)''有什麼區別? – levent