结合通用列表

时间:2013-05-21 13:08:01

标签: c#

现在我将三个列表与一个联合组合在一起。我想用值填充GridView,但问题是在列表中我想要分隔值。例如:

  1. Listitem [0]“Firstname +”“+ Lastname”
  2. Listitem 1 employeeId
  3. Listitem [2]“Second emp Firstname +”“+ second emp姓氏”
  4. Listitem [3] Second emp employeeId
  5. 我希望它能在我的GridView上显示。这是我目前的代码:

    List<string> ulist = _searchEmpList.Union(_searchTechSkillList).Union(_searchAssignList).ToList();
    string s = string.Empty;
    
    for (int i = 0; i < ulist.Count; i++)
    {
        s += ulist[i] + ",";
    }
    
    string[] split = s.Split(',');
    DataRow dr = _dt.NewRow();
    dr["Name"] = split[0]; //firstname + +lastname
    dr["EmployeeId"] = split[1]; //employeeid
    
    _dt.Rows.Add(dr);
    
    Session["DataTableSearch"] = _dt;
    
    GridViewSearchResults.DataSource = _dt;
    GridViewSearchResults.DataBind();
    

    enter image description here

2 个答案:

答案 0 :(得分:1)

我真的不明白为什么你会遇到那些麻烦。以下将按照您的要求进行操作。这是否是你真正想要的是一个不同的问题。

List<string> ulist = _searchEmpList.Union(_searchTechSkillList).Union(_searchAssignList).ToList();
int i = 0;
while (i < ulist.Count-1)
{
    DataRow dr = _dt.NewRow();
    dr["Name"] = ulist[i]; //firstname + +lastname
    dr["EmployeeId"] = ulist[i+1]; //employeeid
    _dt.Rows.Add(dr);
    ++i;
}

使用您要求的循环复制原始代码中的逻辑。它是否会给你结果你认为它应该是一个悬而未决的问题。 Union合并了两个列表,并使用 distinct 值创建了一个新列表。因此,如果两个列表中存在任何重复值,则它们只会在结果中显示一次。

此外,Union不保证最终列表中的项目顺序。

答案 1 :(得分:0)

这里是你要求^^

的例子
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;

namespace simpleOO
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            var myEmployee = new List<Employee>();

            var fistEmployee = new Employee();

            fistEmployee.FirstName = "Filip";
            fistEmployee.LastName = "Göndek";
            fistEmployee.TechSkills.Add(new TechSkills(){Skill="Engish"});
            fistEmployee.Id=1;
            myEmployee.Add(fistEmployee);
            myEmployee.Add(new Employee()
                                           {
                                               Id = 2,
                                               FirstName = "Helmut",
                                               LastName = "Müller",
                                               TechSkills = new List<TechSkills>()
                                                                                {
                                                                                    new TechSkills(){Skill="Engish"},
                                                                                    new TechSkills(){Skill="Spain"}
                                                                                }
                                           }
                                           );

            var filteredList = myEmployee.Where(employee => employee.TechSkills.Any(skill => skill.Skill == "Spain")).ToList();

            var bList = new BindingList<Employee>(filteredList);
            dataGridView1.DataSource = bList;
        }




    }

    public class Employee
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        private List<TechSkills> techSkills = new List<TechSkills>();
        public List<TechSkills> TechSkills
        {
            get { return techSkills; }
            set { techSkills = value; }
        }

        // what ever more you need ...
    }

    public class TechSkills
    {
        public string Skill { get; set; }
    }
}

我知道它非常简单和肮脏,但我认为你得到我想告诉你的东西;)