F # - POCO class

Hey there! I am trying to write a POCO class in the correct F # ... But something is wrong.

C # code that I want to translate to the correct F #:

public class MyTest { [Key] public int ID { get; set; } public string Name { get; set; } } 

Closest I can go to the above code in F #, for example:

 type Mytest() = let mutable _id : int = 0; let mutable _name : string = null; [<KeyAttribute>] member x.ID with public get() : int = _id and public set(value) = _id <- value member x.Name with public get() : string = _name and public set value = _name <- value 

However, when I try to access the properties of the F # version, it just returns a compilation error saying

"Search for an object of an indefinite type based on information up to this point in the program. A type annotation may be required before this program item to restrict the type of object. This may allow the search to be allowed.

The code trying to get the property is part of my repository (I use EF code first).

 module Databasethings = let GetEntries = let ctx = new SevenContext() let mydbset = ctx.Set<MyTest>() let entries = mydbset.Select(fun item -> item.Name).ToList() // This line comes up with a compile error at "item.Name" (the compile error is written above) entries 

What the hell is going on?

Thanks in advance!

+4
source share
1 answer

Defining your class in order is a LINQ problem that has problems. The Select method expects an argument of type Expression<Func<MyTest,T>> , but you pass it a value of type FSharpFunc<MyTest,T> - or something similar to this anyway.

The fact is that you cannot use F # lambda expressions directly with LINQ. You need to write your expression as F # Quotation , and then use F # PowerPack to run the code with the IQueryable<> data source. Don Xime has a good overview of how this works .

+7
source

Source: https://habr.com/ru/post/1333838/


All Articles