我需要查询帮助。我想返回一个实体列表,对于每个实体,我需要两个计数。
使用从这些表生成的实体:
/****** Objet : Table [dbo].[Messages] Date de génération du script : 03/12/2013 15:31:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE [dbo].[Messages]
Go
CREATE TABLE [dbo].[Messages](
[MessageID] [int] IDENTITY(1,1) NOT NULL,
[UserCreation] [nvarchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[DateCreation] [datetime] NOT NULL DEFAULT (getdate()),
[UserModif] [nvarchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[DateModif] [datetime] NOT NULL DEFAULT (getdate()),
[Timestamp] [timestamp] NOT NULL,
[Notification] [bit] NOT NULL DEFAULT ((0)),
[BounceTraite] [bit] NOT NULL DEFAULT ((0)),
CONSTRAINT [PK_Messages] PRIMARY KEY CLUSTERED
(
[MessageID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Objet : Table [dbo].[MessageLocalisations] Date de génération du script : 03/12/2013 15:31:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
DROP TABLE [dbo].[MessageLocalisations]
Go
CREATE TABLE [dbo].[MessageLocalisations](
[MessageID] [int] NOT NULL,
[CodeLocalisation] [char](5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[From] [nvarchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Subject] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Message] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[TargetedAudience] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_MessageLocalisations] PRIMARY KEY CLUSTERED
(
[MessageID] ASC,
[CodeLocalisation] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Objet : Table [dbo].[MessageRecipients] Date de génération du script : 03/12/2013 15:31:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
DROP TABLE [dbo].[MessageRecipients]
Go
CREATE TABLE [dbo].[MessageRecipients](
[MessageID] [int] NOT NULL,
[Recipient] [nvarchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[DateRead] [datetime] NULL,
[Email] [nvarchar](60) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Bounce] [bit] NOT NULL,
[DateCreation] [datetime] NOT NULL,
[DateModif] [datetime] NOT NULL ,
CONSTRAINT [PK_MessageRecipients] PRIMARY KEY CLUSTERED
(
[MessageID] ASC,
[Recipient] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
在我的示例中,我的实体是消息。我需要一个收件人计数和一个已阅读邮件的收件人计数:
(From msg In context.Messages.Include("MessageLocalisations")
Let countRecipients = msg.MessageRecipients.Count
Let countRecipientsRead = msg.MessageRecipients.Where(Function(x) x.DateRead.HasValue).Count
Where (msg.UserCreation.Equals(user, StringComparison.OrdinalIgnoreCase))
Group By msg.MessageID Into Group
Select msg, Group).ToList()
我的例子不起作用。我的选择语句有误,他不认识味精。在 LINQ 中,当我使用 Group By 时,我不确定 Group 的工作方式以及 select 的工作方式。
我想要的结果是一个消息列表,其中包括 MessageLocalisations、每条消息的收件人数量、已阅读每条消息的收件人数量。
谢谢你的帮助!在 SQL 中,这很简单...... :(