我正在尝试将LINQ转换为SQL从C#转换为F#。这就是我想出来的:
let get (roles : List<string>) =
query {
for t in db.Tabs do
join r in db.Regions on (t.Id = r.FkTab)
join l in db.Links on (r.Id = l.FkRegion)
where (roles.Contains(l.Role) || roles.Contains("Administrator"))
groupValBy (t,r,l) (t.Id, t.Label) into tt
select {
Label = snd tt.Key;
Regions = query {
for xr in db.Regions do
join xl in db.Links on (xr.Id = xl.FkRegion)
where (xr.FkTab.Equals (fst tt.Key) && (roles.Contains(xl.Role) || roles.Contains("Administrator")))
groupValBy (xr, xl) (xr.Id, xr.Label) into rr
select {
Label = snd rr.Key;
Links = query {
for yl in db.Links do
where (yl.FkRegion.Equals (fst rr.Key) && (roles.Contains(yl.Role) || roles.Contains("Administrator")))
select {
Label = yl.Label;
Url = yl.Url;
Role = yl.Role
}
} |> Seq.toList
}
} |> Seq.toList
}
} |> Seq.toList
而不是我使用过记录的类,这里是:
type Link = {Url : string; Role : string; Label : string}
type Region = {Label : string; Links : List<Link>}
type Tab = {Label : string; Regions : List<Region> }
不幸的是我在运行时遇到异常:
A first chance exception of type 'System.NotSupportedException' occurred in System.Data.Linq.dll
消息是:System.NotSupportedException:不支持查询运算符'AsQueryable'。
我对F#非常环保,我错过了什么?也许我不能在LINQ to SQL中使用记录?我不能以那种方式筑巢?
由于
编辑:
db
是数据库上下文:
let db = SqlConnection.GetDataContext()
如果你想在家里试试这个,这里是创建所需三个表的脚本:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Links](
[id] [int] IDENTITY(1,1) NOT NULL,
[fkRegion] [int] NOT NULL,
[label] [varchar](50) NOT NULL,
[role] [varchar](50) NOT NULL,
[url] [varchar](100) NOT NULL,
CONSTRAINT [PK_Links] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Regions] Script Date: 22/08/2015 12:06:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Regions](
[id] [int] IDENTITY(1,1) NOT NULL,
[fkTab] [int] NOT NULL,
[label] [varchar](50) NOT NULL,
CONSTRAINT [PK_Regions] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Tabs] Script Date: 22/08/2015 12:06:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Tabs](
[id] [int] IDENTITY(1,1) NOT NULL,
[label] [varchar](50) NOT NULL,
CONSTRAINT [PK_Tabs] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Links] WITH CHECK ADD CONSTRAINT [FK_Links_Regions] FOREIGN KEY([fkRegion])
REFERENCES [dbo].[Regions] ([id])
GO
ALTER TABLE [dbo].[Links] CHECK CONSTRAINT [FK_Links_Regions]
GO
ALTER TABLE [dbo].[Regions] WITH CHECK ADD CONSTRAINT [FK_Regions_Tabs] FOREIGN KEY([fkTab])
REFERENCES [dbo].[Tabs] ([id])
GO
ALTER TABLE [dbo].[Regions] CHECK CONSTRAINT [FK_Regions_Tabs]
GO
EDIT2: 问题似乎与查询的结果有关。我尝试将记录类型更改为IEnumerable并删除Seq.toList。但是,每当我尝试对可枚举的任何操作时,都会弹出相同的异常,例如, Model.Count()(其中Model是IEnumerable)。帮助