C# 中的联合两个列表
我想联合、合并到一个包含两个引用的列表中,所以这是我的代码,我如何定义一个准备好这个海豚的列表?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想将
AftList2
中的所有条目添加到lstAux
,您应该将AftList2
定义为 IEnumerable<> 具有DetalleCita
类型的元素(IEnumerable
足以用作List上
)。 例如这样:AddRange()
的参数;然后您可以将其所有元素添加到
lstAux
:澄清:
我认为您误解了扩展方法
ToList()
的作用。 它从IEnumerable
创建新列表,并且其结果与应用它的原始IEnumerable
不相关。这就是为什么你只是尝试做任何有用的事情
list.ToList().AddRange(...)
- 你正在将列表复制到(另一个由ToList() 新创建的列表
) code>) 列表,更新它,然后基本上扔掉它(因为你甚至没有做像list2 = var1.ToList()
这样的事情,原始的var1
之后保持不变!!! 如果您调用它,您很可能希望保存ToList()
的结果)。此外,您通常不需要将一个列表转换为另一个列表,当您需要列表 (
List
) 但有IEnumerable< 时,
(不可索引,您可能需要通过索引快速访问,或者延迟计算,但您需要此时计算出所有结果 - 在尝试使用 LINQ to 对象查询的结果时,这两种情况都可能出现示例:ToList()
非常有用。 ;T>IEnumerableints = from i in anotherInts where i > 20 select i;
-- 即使anotherInts
是List
查询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)
返回新集合,并且不会更新a
或b
)。If you want to add all entries from
AftList2
tolstAux
you should defineAftList2
as IEnumerable<> with elements of typeDetalleCita
(beingIEnumerable<DetalleCita>
is enough to be used as parameter ofAddRange()
onList<DetalleCita>
). For example like this:And then you can add all its elements to
lstAux
:Clarification:
I think you are misunderstanding what extension method
ToList()
does. It creates new list fromIEnumerable<T>
and its result is not connected with originalIEnumerable<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 byToList()
) list, update it and then basically throwing away it (because you are not even doing something likelist2 = var1.ToList()
, originalvar1
stays unchanged after that!!! you most likely want to save result ofToList()
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 haveIEnumerable<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 ifanotherInts
wasList<int>
result of queryints
cannot be cast toList<int>
because it is not list but implementation ofIEnumerable<int>
. In this case you could useToList()
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 useUnion()
extension method instead ofAddRange
(I don't think this is better than first solution and be careful because it works more likeToList()
--a.Union(b)
return new collection and does NOT updates eithera
orb
).