如何在EF中添加var项?

时间:2017-05-08 20:25:47

标签: c# entity-framework-4

我有一个数组,其长度未指定。也许是100,110,......

我拥有的每个值:

  int a1 = array[0];
    var r1 = from row in db.products
    where row.id == a1
    select row;
    int a2 = array[1];
    var r2 = from row in db.products
    where row.id == a2
    select row;
...

如何得到(r1 + r2 + r3 + ...(= sum))的总和?

总结之后,我想序列化:

string s1 = js.Serialize(sum);

1 个答案:

答案 0 :(得分:1)

您可以在代码中执行此操作:

表:

SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RowId

程序:

--use your db
USE [Breaz]
GO
/****** Object:  Table [dbo].[theProducts]    Script Date: 5/8/2017 3:49:33 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[theProducts](
    [theId] [int] IDENTITY(1,1) NOT NULL,
    [id] [int] NULL,
 CONSTRAINT [PK_theProducts] PRIMARY KEY CLUSTERED 
(
    [theId] 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 IDENTITY_INSERT [dbo].[theProducts] ON 

GO
INSERT [dbo].[theProducts] ([theId], [id]) VALUES (1, 5)
GO
INSERT [dbo].[theProducts] ([theId], [id]) VALUES (2, 6)
GO
INSERT [dbo].[theProducts] ([theId], [id]) VALUES (3, 7)
GO
SET IDENTITY_INSERT [dbo].[theProducts] OFF
GO