日期:2014-05-20  浏览次数:20699 次

LINQ里的Union和Concat,不管用,啥原因呢??

        IList<DepartmentTree> list0 = BLLDepartmentTree.Instance.FindAll().Where(o => o.ID == 0).ToList();
        this.Response.Write(string.Format("list0:{0};<br /><br />", list0.Count));

        IList<DepartmentTree> list1 = BLLDepartmentTree.Instance.FindAll().Where(o => o.ID == 1).ToList();
        list0.Concat(list1);
        this.Response.Write(string.Format("list1:{0};<br />", list1.Count));
        this.Response.Write(string.Format("list0:{0};<br /><br />", list0.Count));

        IList<DepartmentTree> list2 = BLLDepartmentTree.Instance.FindAll().Where(o => o.ID == 5).ToList();
        list0.Concat(list2);
        this.Response.Write(string.Format("list2:{0};<br />", list2.Count));
        this.Response.Write(string.Format("list0:{0};<br /><br />", list0.Count));


我是用的linq to object。为什么那个list0.Count搞来搞去,一直是 =1 呀。

list0.Concat(list1);我也用Union了,都不行呀。好像并没起到作用。啥原因呢?

PS:数据里绝对没出现相同的两行,可是我也用Concat了呀。麻烦各位看看呀,谢谢

------最佳解决方案--------------------
"o => o.ID == 0"

我没猜错的话,ID应该是你的主键。
那查出来Count结果不是1条记录是什么呢?
------其他解决方案--------------------

            List<Myclass> result =
                DataContext.Reward_award.Where(x => x.sports.Contains(strQuery)).Select(x => x).ToList();

            List<Myclass> a = DataContext.Reward_award.Where("1=1").ToList();
            
            List<Myclass> bbb = result.Concat(a).ToList();



你看看我项目里的代码。我的bbb.Count 是正常的。
------其他解决方案--------------------
list0.Concat(list1).TOList();
试试看。
------其他解决方案--------------------
你犯了一个低级错误,你没有赋值。
list0=list0.Concat(list1).ToList();
------其他解决方案--------------------
list0.Concat(list1);

list0.Concat(list2);

谢谢flysnow_z呀。我知道id == n,是只得到一条记录。

可是我有这么两条语句呀。不是合并了他们的结果集了吗?

那最后输出list0,相当于3个条记录的合并一起的呀。难道我理解错了吗??
------其他解决方案--------------------
引用:
你犯了一个低级错误,你没有赋值。 
list0=list0.Concat(list1).ToList();


晕死,才发现,我说咋怎么都不管用呀!!!

不过语法还真奇怪,为什么还需要再赋值,直接list0.Concat不就合并了就可以了。

非要再赋值下,哎~奇怪的linq。谢谢flysnow~~

两天结贴,算是给大家一个反面教材吧。
------其他解决方案--------------------
没有赋值我自己也出现过,发现后自己都觉得好笑,哈哈
------其他解决方案--------------------