C# 中的联合两个列表

发布于 2024-07-10 13:40:35 字数 1204 浏览 13 评论 0原文

我想联合、合并到一个包含两个引用的列表中,所以这是我的代码,我如何定义一个准备好这个海豚的列表?

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

这是在 rowcommand 事件内部。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七婞 2024-07-17 13:40:35

如果您想将 AftList2 中的所有条目添加到 lstAux,您应该将 AftList2 定义为 IEnumerable<> 具有 DetalleCita 类型的元素(IEnumerable 足以用作 ListAddRange() 的参数;)。 例如这样:

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

然后您可以将其所有元素添加到 lstAux

lstAux.AddRange(AftList2);

澄清:

我认为您误解了扩展方法 ToList() 的作用。 它从 IEnumerable 创建新列表,并且其结果与应用它的原始 IEnumerable 不相关。

这就是为什么你只是尝试做任何有用的事情 list.ToList().AddRange(...) - 你正在将列表复制到(另一个由 ToList() 新创建的列表) code>) 列表,更新它,然后基本上扔掉它(因为你甚至没有做像 list2 = var1.ToList() 这样的事情,原始的 var1 之后保持不变!!! 如果您调用它,您很可能希望保存 ToList() 的结果)。

此外,您通常不需要将一个列表转换为另一个列表,当您需要列表 (List) 但有 IEnumerable< 时,ToList() 非常有用。 ;T> (不可索引,您可能需要通过索引快速访问,或者延迟计算,但您需要此时计算出所有结果 - 在尝试使用 LINQ to 对象查询的结果时,这两种情况都可能出现示例: IEnumerableints = from i in anotherInts where i > 20 select i; -- 即使 anotherIntsList 查询ints的结果无法转换List,因为它不是列表而是IEnumerable<的实现在这种情况下,您可以使用 ToList() 来获取列表: Listints = (from i in anotherInts where i > 20 select i).ToList( );)。

更新:

如果您真的指的是联合语义(例如,对于 { 1, 2 } 和 { 1, 3 } 联合 将类似于 { 1, 2, 3 },两个元素中的相等元素不会重复集合)考虑切换到 HashSet (它很可能适合您的情况,因为您使用的是 C# 3.0 并且我想您有最新的 .NET 框架)或使用 Union()< /code> 扩展方法而不是 AddRange (我认为这并不比第一个解决方案更好,并且要小心,因为它的工作方式更像 ToList() -- a.Union(b) 返回新集合,并且不会更新 ab)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文