krisal
krisal

Reputation: 631

Combining generic lists

Right now I'm combining three lists with an union. I want to populate a GridView with the values, but the problem here is that in the list I want to separate the values. For example:

  1. Listitem [0] "Firstname + " " + Lastname"
  2. Listitem 1 employeeId
  3. Listitem [2] "Second emp Firstname + " " + Second emp Lastname"
  4. Listitem [3] Second emp employeeId

I want it to show on my GridView. This is my code at the moment:

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

Upvotes: 0

Views: 149

Answers (2)

WiiMaxx
WiiMaxx

Reputation: 5420

here is the example you where asking for ^^

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

i know it is very simple and dirty but i think you get what i want to tell you ;)

Upvotes: 0

Jim Mischel
Jim Mischel

Reputation: 133975

I really don't understand why you're going to all that trouble. The following will do what you say you want. Whether it's what you really want is a different question.

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

That duplicates the logic in your original code, with the loop that you asked for. Whether it's going to give you the results you think it should is an open question. Union combines the two lists and creates a new list with distinct values. So if there are any duplicate values in the two lists, they'll only show up once in the result.

Also, Union makes no guarantee about the order of items in the final list.

Upvotes: 1

Related Questions