Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ和EF获取相关对象_C#_Linq_Entity Framework - Fatal编程技术网

C# 使用LINQ和EF获取相关对象

C# 使用LINQ和EF获取相关对象,c#,linq,entity-framework,C#,Linq,Entity Framework,我有两个通过外键关系链接的对象,我使用DataModel映射这些对象: 事件:1-----*:资产 我编写了一个查询,获取给定[eventPublicId]的所有资产 List<Asset> assetList = ReliableExecution.RetryWithExpression<Event, List<Asset>> (u => u.FirstOrDefault(x => x.PublicId == eventPubli

我有两个通过外键关系链接的对象,我使用DataModel映射这些对象:

事件:1-----*:资产

我编写了一个查询,获取给定[eventPublicId]的所有资产

List<Asset> assetList =
    ReliableExecution.RetryWithExpression<Event, List<Asset>>
    (u => u.FirstOrDefault(x => x.PublicId == eventPublicId).Assets.ToList()).ToList();
列表资产列表=
ReliableExecution.RetryWithExpression
(u=>u.FirstOrDefault(x=>x.PublicId==eventPublicId).Assets.ToList()).ToList();
我的问题是我不得不调用ToList()两次,这看起来很尴尬。我还必须使用FirstOrDefault,但当我试图使用[Where]或其他任何东西时,它无法编译

有没有其他更好的方法来重写这段代码

这是RetryWithExpression签名以供参考:

public static TValue RetryWithExpression<T, TValue>(Func<ObjectSet<T>, TValue> func, Int32 retryInfiniteLoopGuard = 0)
         where T : class
public static TValue RetryWithExpression(Func-Func,Int32 RetryInfiniteLopGuard=0)
T:在哪里上课

您指定
func
参数应返回
列表
,因此导航属性
事件.资产
不适合账单:它是
实体集合
,不能隐式转换为委托返回类型。显式转换
ToList()
创建指定的类型

从技术上讲,要摆脱托利表,您应该使用

ReliableExecution.RetryWithExpression<Event, EntityCollection<Asset>> ...
ReliableExecution.RetryWithExpression。。。

但是我不知道这是否满足您的功能要求。

为什么您必须调用
ToList()
两次?您得到了什么编译器错误?如果我省略了第一个Assets.ToList,我会得到一个编译错误:无法将lambda表达式转换为委托类型,因为块中的某些返回类型不能隐式转换为委托返回类型。如果省略第二个ToList,在运行时会出现connection not open错误,但这是出于设计。这是有道理的,但在尝试EntityCollection后会出现错误:“ObjectContext实例已被释放,无法再用于需要连接的操作”。这可能是出于设计原因,因为RetryWithExpression会在返回时处理连接。RetryWithExpression不支持不同的执行,因为我必须采取变通办法来集成瞬态故障处理应用程序块,不幸的是,该应用程序块不完全支持EF,并且我必须显式管理连接。看来我得用ToList来做。谢谢