ASP.NET将项目从类添加到列表 [英] ASP.NET add items to List from Class

查看:119
本文介绍了ASP.NET将项目从类添加到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里上这堂课:

public class CP_VIP_Preview_TimeSlots
    {
        public int id { get; set; }
        [DisplayName("Time Slots")]
        public string timeSlot { get; set; }
        [DisplayName("Date Slots")]
        public string dateSlot { get; set; }
    }

    public class CPVIPPreviewTimeSlots : DbContext
    {
        public DbSet<CP_VIP_Preview_TimeSlots> Data { get; set; }
    }

现在我想做的是将类的每个项目都放入一个列表中,我已经创建了这个列表:

Now what I am trying to do is put each item of the class into a List, I have created this list:

List<string> newList = new List<string>();

我要添加到此列表的是id和timeSlot和DateSlot,但是我希望将timeSlot和DateSlot作为组合字符串.所以我的新列表将具有id和一些名为timeDateSlot的字符串...我希望这有意义,我该怎么做?

What I am looking to add to this list is a the id and timeSlot and DateSlot, however I want the timeSlot and DateSlot as a combined string. So my new list will have id and some string called timeDateSlot...I hope this make sense, how would I do this?

推荐答案

如果您要使用ID标识数据,则可以使用以下代码

If you want to identify data using Id you can use the following code

foreach(CP_VIP_Preview_TimeSlots d in CPVIPPreviewTimeSlots.Data)
{
    List<KeyValuePair<int,string>> lstIdWiseData = new List<KeyValuePair<int,string>>();
    lstIdWiseData.Add(new KeyValuePair<int,string>(d.Id, string.Concat(d.timeSlot, d.dateSlot )));
}

如果您的ID不是唯一的,则可以将上述代码中的 KeyValuePair< int,string> 替换为 Tuple< int,string>

If your id is not unique you can replace KeyValuePair<int,string> with Tuple<int,string> in the above code

这篇关于ASP.NET将项目从类添加到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆