Linq, OData和WCF:“不支持join方法”
本文关键字:不支持 join 方法 OData WCF Linq | 更新日期: 2023-09-27 18:19:17
我试图从多个表中恢复和显示数据。我已经做了一个像这样的多重连接:
var prod = from product in context.PRODUCT
join islocated in context.ISAUDITEDIN on product.PRODUCT_ID equals islocated.PRODUCT_ID
join location in context.LOCATION on islocated.LOCATION_ID equals location.LOCATION_ID
orderby location.LOCATION_ID
group new
{
Location_ID = location.LOCATION_ID,
Product_ID = product.PRODUCT_ID
} by location.LOCATION_ID into locat
select new {
Location_ID = locat.Key,
Product = locat
};
,其中context是导致web服务调用的OData。这个链接正在工作:当我做一个简单的选择时,我能够恢复数据。然后,我想显示结果中的数据。所以我创建了一个dictionary:
Dictionary<string,List<ProductModel>> dict = new Dictionary<string,List<ProductModel>>();
foreach (var locat in prod) {
List<ProdctModel> products = new List<ProductModel>();
foreach (var p in locat.Product)
{
products.Add(new ProductModel(p.Location_ID, p.Product_ID));
}
dict.Add(locat.Location_ID.ToString(), products);
}
其中ProductModel是一个像这样的简单类:
public class ProdctModel
{
public int locationID{get; set;}
public int productID{get; set;}
public ProdctModel(int location_ID, int product_ID)
{
this.locationID = location_ID;
this.productID = product_ID;
}
}
但是当我运行它时,我得到了以下内容:
“the method join is not supported”
Ligne 131 : Dictionary<string,List<ProdctModel>> dict = new Dictionary<string,List<ProdctModel>>();
Ligne 132 : foreach (var locat in prod) {
如何解决?我已经看到了一些事情通过使用方法。expand()而不是join,但我不知道如何使用它:你能帮助我吗?
谢谢!
我从未在linq中使用过odata,但我假设您不能连接不同的远程资源。尝试先将对象放入内存,然后再执行连接:
var products = context.PRODUCT.ToList();
var islocateds = context.ISAUDITEDIN.ToList();
var locations = context.LOCATION.ToList();
var prod = from product in products
join islocated in islocateds on product.PRODUCT_ID equals islocated.PRODUCT_ID
join location in locations on islocated.LOCATION_ID equals location.LOCATION_ID
orderby location.LOCATION_ID
group new
{
Location_ID = location.LOCATION_ID,
Product_ID = product.PRODUCT_ID
} by location.LOCATION_ID into locat
select new {
Location_ID = locat.Key,
Product = locat
};
请记住,这将下载所有3个表。
编辑此外,在创建字典时得到异常的原因是"prod"变量是一个IEnumerable-只有当您执行"foreach"或类似的操作时,它才执行请求。