Union two lists

I want to merge, merge a list containing two links, 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();
            }

      

it's inside the rowcommand event. Thank!

+1


source to share


1 answer


If you want to add all entries from AftList2

to lstAux

, you must define AftList2

as IEnumerable <> with elements of type DetalleCita

(being IEnumerable<DetalleCita>

sufficient to be used as a parameter AddRange()

on List<DetalleCita>

). For example, for example:

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

      

And then you can add all of its elements to lstAux

:

lstAux.AddRange(AftList2);

      

Clarification:



I think you don't understand what the extension method does ToList()

. It creates a new list from IEnumerable<T>

, and its result is unrelated to the original IEnumerable<T>

one it applies to.

This is why you just don't do anything useful trying to do list.ToList().AddRange(...)

- you copy the list (another new created ToList()

) list, update it and then basically discard it (because you don't even do something like list2 = var1.ToList()

, the original var1

remains the same after that !!! you will most likely want to store the result ToList()

if you call it).

Also you usually do not need to convert one list to another list, ToList()

useful when you need a list ( List<T>

), but has IEnumerable<T>

(which is not indexed and you may need fast index access, or lazy evaluations, but you need all the results calculated at this time - the two situations can occur when you attempt to use the result of the query LINQ to objects, for example IEnumerable<int> ints = from i in anotherInts where i > 20 select i;

- even if anotherInts

was a List<int>

result of the request ints

can not be referred to List<int>

because it is not a list, and implementation IEnumerable<int>

In this case, you could use. ToList()

: to get a list List<int> ints = (from i in anotherInts where i > 20 select i).ToList();

).

UPDATE:

If you really mean union semantics (for example, to combine {1, 2} and {1, 3} it would be something like {1, 2, 3}, without duplicating equal elements from two collections) consider going to HashSet<T>

( this is most likely available in your situation, because you are using C # 3.0 and I assume you have a recent .NET framework) or use an extension method Union()

instead AddRange

(I don’t think this is better than the first and be careful because it more like ToList()

- a.Union(b)

returns a new collection and doesn't update a

either b

).

+4


source







All Articles