首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

Intersect ef中的交加

2013-06-26 
Intersectef中的交集我在调试的时候,看到2个list有共同的元素,为什么temp.Count() 0呢? ListDepartment

Intersect ef中的交集


我在调试的时候,看到2个list有共同的元素,为什么temp.Count() =0呢?



 List<Department> temp = deptBranch.Intersect(deptList).ToList();
                                if (deptBranch.Intersect(deptList).ToList().Count() >= 2)
                                    continue;








完整


        public static bool AddTask(Task task,int[] deptArr=null)
        {
            try
            {
                using (var db = new WEBVODEntities())
                {
                    string loginName = CurrentProject.GetCurrentUser().LoginName;

                    //添加-Task-对象
                    task.TimeCreate = DateTime.Now;
                    task.ManCreate = loginName;
                    task.LoginName = loginName;
                    task.Guid = Guid.NewGuid().ToString();
                    db.Task.AddObject(task);


                    //添加-部门-任务
                    if (deptArr != null && (task.TypeId == Constants.TaskType_Global || task.TypeId == Constants.TaskType_Local))   
                    {
                        List<Department> deptList = db.Department.Where(a => deptArr.Contains(a.Id)).OrderBy(a=>a.Level).ToList(); //级别高的排前边


                        //添加-TaskDept-对象
                        foreach (var dept in deptList)
                        {
                            //父节点已经被添加,就不再添加。


                            if (task.TypeId == Constants.TaskType_Global)
                            {
                                List<Department> deptBranch = DepartmentBLL.GetBranch(dept.Id);   //!!!思考:每次都要数据库查询一次,有没有什么算法,可以只查询一次.
                                List<Department> temp = deptBranch.Intersect(deptList).ToList();
                                if (deptBranch.Intersect(deptList).ToList().Count() >= 2)
                                    continue;
                            }
                            

                            TaskDept taskDept = new TaskDept();
                            taskDept.Task = task;
                            taskDept.Department = dept;
                            taskDept.TimeCreate = DateTime.Now;
                            //db.TaskDept.AddObject(taskDept);
                        }
                    }

                    //添加-个人-任务
                    if(task.TypeId == Constants.TaskType_Per)
                    {
                        TaskDept taskDept = new TaskDept();
                        taskDept.Task = task;
                        taskDept.LoginName = loginName;


                        taskDept.TimeCreate = DateTime.Now;
                        //db.TaskDept.AddObject(taskDept);
                    }

                    db.SaveChanges();
                }
            }
            catch
            { 
            }

            return false;
        }


[解决办法]
deptBranch.Intersect(deptList).ToList(); 
这个Intersect里面的对象deptList已经是集合了,请换成在Intersect这个里面是Department这个对象。

如果Intersect里面只能放集合对象的话,那么用deptBranch.Add(Department对象),再试试看行不行。
[解决办法]
必须是同一个对象的引用才会产生所谓的交集 这就是值类型和引用类型的区别,看如下实例
 Person publicPerson = new Person() { Id = 1, Name = "a", Count = 1 }; 
                List<Person> list1 = new List<Person>() { 
                    new Person(){Id=1,Name="a",Count=1},
                    new Person(){Id=2,Name="a",Count=1},
                    new Person(){Id=3,Name="a",Count=1}
                };
                List<Person> list2 = new List<Person>() { 
                    new Person(){Id=1,Name="a",Count=1}
                };

                int count1 = list1.Intersect(list2).Count();//0

                List<Person> list3 = new List<Person>() { 
                    publicPerson,


                    new Person(){Id=2,Name="a",Count=1},
                    new Person(){Id=3,Name="a",Count=1}
                };
                List<Person> list4 = new List<Person>() { 
                    publicPerson
                };
               int count2 = list3.Intersect(list4).Count();//1


[解决办法]
你的问题其实很简单,以Product类的两个集合为例:
Product[] store1 = { new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "orange", Code = 4 } };

Product[] store2 = { new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "lemon", Code = 12 } };


...


// Get the products from the first array  
// that have duplicates in the second array.

IEnumerable<Product> duplicates =
    store1.Intersect(store2, new ProductComparer());

foreach (var product in duplicates)
    Console.WriteLine(product.Name + " " + product.Code);

/*
    This code produces the following output:
    apple 9
*/



public class Product
{
    public string Name { get; set; }
    public int Code { get; set; }
}

// Custom comparer for the Product class 
class ProductComparer : IEqualityComparer<Product>
{
    // Products are equal if their names and product numbers are equal. 
    public bool Equals(Product x, Product y)
    {

        //Check whether the compared objects reference the same data. 
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null. 
        if (Object.ReferenceEquals(x, null) 
[解决办法]
 Object.ReferenceEquals(y, null))


            return false;

        //Check whether the products' properties are equal. 
        return x.Code == y.Code && x.Name == y.Name;
    }

    // If Equals() returns true for a pair of objects  
    // then GetHashCode() must return the same value for these objects. 

    public int GetHashCode(Product product)
    {
        //Check whether the object is null 
        if (Object.ReferenceEquals(product, null)) return 0;

        //Get hash code for the Name field if it is not null. 
        int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

        //Get hash code for the Code field. 
        int hashProductCode = product.Code.GetHashCode();

        //Calculate the hash code for the product. 
        return hashProductName ^ hashProductCode;
    }

}

热点排行