I am learning LINQ with F# 3.0. Try to follow the examples by “Query Expressions (F#)” from this URL: http://msdn.microsoft.com/en-us/library/hh225374%28v=vs.110%29.aspx
I have created a simple data table in SQL Server 2008 R2, the database name is myDatabase.
Create a simple data table as indicated in the sample:
CREATE TABLE [dbo].[Student] (
[StudentID] INT NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[Age] INT NULL,
PRIMARY KEY CLUSTERED ([StudentID] ASC)
);
Then add a few rows:
INSERT INTO Student (StudentID, Name, Age)
VALUES(1, ‘Abercrombie, Kim’, 10);
INSERT INTO Student (StudentID, Name, Age)
VALUES(2, ‘Abolrous, Hazen’, 14);
INSERT INTO Student (StudentID, Name, Age)
VALUES(3, ‘Hance, Jim’, 12);
INSERT INTO Student (StudentID, Name, Age)
VALUES(4, ‘Adams, Terry’, 12);
INSERT INTO Student (StudentID, Name, Age)
VALUES(5, ‘Hansen, Claus’, 11);
Try to understand what is groupBy in the sample, the following is my code:
#light
open System
open System.Data
open System.Data.Linq
open Microsoft.FSharp.Data.TypeProviders
open Microsoft.FSharp.Linq
[<Generate>]
type dbSchema = SqlDataConnection<"Data Source=.;Initial Catalog=myDatabase;Integrated Security=True">
let db = dbSchema.GetDataContext()
let groupX =
query {
for student in db.Student do
groupBy student.Age into g
select (g.Key, g.Count())
}
However, I got compiler error:
Error The field, constructor or member ‘Count’ is not defined
I am thinking Count() is a built-in function, but it is not.
Tell me how I can use the sample code. Or, if it is a mistake, show me the code can do the job.
By the way, I believe another sample: groupValBy has the same issue.
The code sample from the web site is:
query {
for student in db.Student do
groupValBy student.Name student.Age into g
select (g, g.Key, g.Count())
}
Let me know if I miss something or think wrongly.
Thanks,
John
My guess is that you miss
The
Count()is an extension method defined there.