我需要查询帮助。我想返回一个实体列表,对于每个实体,我需要两个计数。
使用表格生成实体:
/****** 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()
我的例子不起作用。我的select语句错误,他不识别msg。在LINQ中,我不确定Group如何工作以及当我使用Group By时如何选择工作。
我想要的结果是一个消息列表,其中包括MessageLocalisations,每封邮件的收件人数,读取每封邮件的收件人数。
感谢您的帮助!在SQL中这很简单...... :(
答案 0 :(得分:0)
这是最终解决我问题的查询:
context.MessageSecuriseJeu.Include("MessageSecuriseLocalisations").
Where(Function(w) w.UtilisateurCreation.Equals(codeUtilisateurExpediteur, StringComparison.CurrentCultureIgnoreCase)).
Select(Function(msg) New With {.msg = msg, .nbrDest = msg.MessageSecuriseDestinataires.Count, .nbrLu = msg.MessageSecuriseDestinataires.Where(Function(w) w.DateLecture.HasValue).Count}).tolist()