在 f# 中使用 IRepository 接口

Sky*_*ell 1 f# c#-to-f# f#-3.0

我目前正在将一个用 c# 编写的研究生项目重写为 f#。

我对如何处理 f# 中的 IRepository 接口感到困惑。这在 c# 中似乎微不足道,但 f# 不喜欢我所做的。

所讨论的 IRepository 是在 myNameSpace.SolarSystem 名称空间中定义的。我确保将其包含在我的 f# 项目中。

这是我的笔记:

f# - 接口类型的无效使用<--( let repo = IRepository<SolarSystem>())

open myNameSpace.SolarSystem

let searchCatalog = [| 8; 11; 31 |]

let repo = IRepository<SolarSystem>()

let ClassOfSolarSystems classOfStar = 
    repo.Query().Where(fun s -> s.SolarGroups.Any(fun c -> searchCatalog.Contains(classOfStar) ))
Run Code Online (Sandbox Code Playgroud)

c# - 没有错误:

using myNameSpace.SolarSystem

private readonly int[]  searchCatalog = new int[] { 8, 11, 31 };

public IRepository<SolarSystem> Repo { get; set; }

public IEnumerable<SolarSystem> ClassOfSolarSystems(Int32 classOfStar)
{
    return Repo.Query()
    .Where(s => s.SolarGroups.Any(c =>   searchCatalog.Contains(classOfStar)));
}
Run Code Online (Sandbox Code Playgroud)

我用尽了我的谷歌,找不到任何有意义的(至少对我来说)解决方案。

有没有办法在 f# 中使用 IRepository 接口?

谢谢!

asi*_*ahi 5

您的 F# 行与此 C# 等效,因为该关键字new在 F# 中是隐式的。

var repo = new IRepository<SolarSystem>()
Run Code Online (Sandbox Code Playgroud)

C# 编译器也不会让您这样做。此外,您的 C# 示例是一个属性,而在 F# 中它是一个值绑定。要在 F# 中定义属性,您需要使用关键字member

编辑

我对编辑器进行了一些尝试,直到我发现编译器有点满意的东西,并提出了这个。

let ClassOfSolarSystems (repo : IRepository<SolarSystem>) classOfStar = 
    repo.Query()
    |> Seq.filter(fun s -> s.SolarGroups.Any(fun c -> searchCatalog.Contains(classOfStar) ))
Run Code Online (Sandbox Code Playgroud)

我没有过多地修改您的 LINQ 表达式,但您应该考虑使用F#Seq模块