views:

873

answers:

1

Hi there, i want to union, merge in a List that contains both references, so this is my code, how can i define a list ready for this porpouses ?

if (e.CommandName == "AddtoSelected")
            {
                List<DetalleCita> lstAux = new List<DetalleCita>();
                foreach (GridViewRow row in this.dgvEstudios.Rows)
                {
                    var GridData = GetValues(row);
                    var GridData2 = GetValues(row);
                    IList AftList2 = GridData2.Values.Where(r => r != null).ToList();
                    AftList2.Cast<DetalleCita>();
                    chkEstudio = dgvEstudios.Rows[index].FindControl("ChkAsignar") as CheckBox;
                    if (chkEstudio.Checked)
                    {

                        IList AftList = GridData.Values.Where(r => r != null).ToList();
                        lstAux.Add(
                        new DetalleCita
                        {
                            codigoclase = Convert.ToInt32(AftList[0]),
                            nombreestudio = AftList[1].ToString(),
                            precioestudio = Convert.ToDouble(AftList[2]),
                            horacita = dt,
                            codigoestudio = AftList[4].ToString()
                        });
                    }
                    index++;
                    //this line to merge
                    lstAux.ToList().AddRange(AftList2);
                }

                dgvEstudios.DataSource = lstAux;
                dgvEstudios.DataBind();
            }

this is inside a rowcommand event. Thanks!

+2  A: 

If you want to add all entries from AftList2 to lstAux you should define AftList2 as IEnumerable<> with elements of type DetalleCita (being IEnumerable<DetalleCita> is enough to be used as parameter of AddRange() on List<DetalleCita>). For example like this:

var AftList2 = GridData2.Values.Where(r => r != null).Cast<DetalleCita>();

And then you can add all its elements to lstAux:

lstAux.AddRange(AftList2);

Clarification:

I think you are misunderstanding what extension method ToList() does. It creates new list from IEnumerable<T> and its result is not connected with original IEnumerable<T> that it is applied to.

That is why you are just do nothing useful trying to do list.ToList().AddRange(...) - you are copying list to (another newly created by ToList()) list, update it and then basically throwing away it (because you are not even doing something like list2 = var1.ToList(), original var1 stays unchanged after that!!! you most likely want to save result of ToList() if you are calling it).

Also you don't usually need to convert one list to another list, ToList() is useful when you need list (List<T>) but have IEnumerable<T> (that is not indexable and you may need fast access by index, or lazy evaluates but you need all results calculated at this time -- both situations may arise while trying to use result of LINQ to objects query for example: IEnumerable<int> ints = from i in anotherInts where i > 20 select i; -- even if anotherInts was List<int> result of query ints cannot be cast to List<int> because it is not list but implementation of IEnumerable<int>. In this case you could use ToList() to get list anyway: List<int> ints = (from i in anotherInts where i > 20 select i).ToList();).

UPDATE:

If you really mean union semantics (e.g. for { 1, 2 } and { 1, 3 } union would be something like { 1, 2, 3 }, with no duplication of equal elements from two collections) consider switching to HashSet<T> (it most likely available in your situation 'cause you are using C# 3.0 and I suppose yoou have recent .NET framework) or use Union() extension method instead of AddRange (I don't think this is better than first solution and be careful because it works more like ToList() -- a.Union(b) return new collection and does NOT updates either a or b).

IgorK