简体   繁体   English

如何在EF中添加var项目?

[英]How can i add var item in EF?

I have an array that its length is unspecified. 我有一个数组,其长度未指定。 Maybe 100 , 110 , ... 也许100,110,...

for each value i have : 对于每个值我都有:

  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;
...

How can i get summation of (r1 + r2 + r3 + ... (= sum))? 如何获得(r1 + r2 + r3 + ...(=和))的总和?

After this summation, i want to serialize: 总结之后,我想序列化:

string s1 = js.Serialize(sum);

You can do this in code: 您可以在代码中执行此操作:

Table: 表:

--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

program: 程序:

//use your database dbcontext
       using (BreazEntities21 db = new BreazEntities21())
        {
            int[] a1 = { 5, 6 };
            var r1 = (from row in db.theProducts
                     where a1.Contains((int)row.id)
                     select row).ToList();
            int[] a2 = { 7 };
            var r2 = (from row in db.theProducts
                     where a2.Contains((int)row.id)
                     select row).ToList();
            var mergedList = r1.Union(r2).ToList();
            var summation = mergedList.Sum(r => r.id);
            var s1 = new JavaScriptSerializer().Serialize(summation);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用EF4将新项目添加到EntitySet? - How can I add a new item to a EntitySet Using EF4? 如何将条件字符串添加到 var 类型的 sql 查询中 - How can i add a condition string to an sql query in an var type 如何动态地将继承的属性添加到EF数据存储中? - How can I dynamically add inherited properties to EF datastore? 如何在EF中的包含实体中添加where子句? - How can I add a where clause to an in included entity in EF? 如何将项目添加到 IEnumerable&lt;(T item, int? number)&gt;? - How can I add an item to a IEnumerable<(T item, int? number)>? 如何将新对象添加到现有实体,将数据放入数据中并让EF将其添加到数据库中? - How can I add a new object to an existing Entity, PUT the data and have EF add it to my database? 如何在Rss的“项目元素”中添加图像? - How can i add image in “item element” in Rss? 如何使用Xamarin和C#将项目添加到macOS NSComboBox? - How Can I Add Item to macOS NSComboBox with Xamarin & C#? 如何将WPF表单添加到选项卡控件的选项卡项中 - How can I add WPF form into tab item of tab control 如果选中复选框,我如何将“项目”添加到 arraylist? - How can I add “item” to arraylist, if checkbox is marked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM