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

LINQ里的Union和Concat,不管用,啥原因呢??.
C# code

        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条记录是什么呢?
------解决方案--------------------
C# code

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

------解决方案--------------------
list0.Concat(list1).TOList();
试试看。
------解决方案--------------------
你犯了一个低级错误,你没有赋值。
list0=list0.Concat(list1).ToList();