如果值位于不同的表中,则Linq查询不会返回正确的结果

时间:2014-09-08 09:05:26

标签: c# sql asp.net-mvc linq entity-framework

您好:我有两张桌子。

表格

Person table:
--------------------------------
| id | Namer | Surename | City |
--------------------------------
|1   |aaa    |aaa       | NewY |
|2   |bbb    |bbb       | Dall |
|3   |ccc    |ccc       | Dall |
|4   |ddd    |ddd       | Dall |
--------------------------------

Job table:
-------------------------
| id | PersonID | JobID |
-------------------------
|1   |1         |1      |
|2   |3         |1      |
|3   |2         |2      |
|4   |3         |2      |
-------------------------

我现在的代码:

C#:

public IEnumerable<Material> GetAllMaterialsByTypeNotSelected(string type , int id)
{
    return (from m in dataContext.Person
            from cfm in dataContext.Job
            where m.Id != cfm.PersonID && 
            m.City == type &&
            cfm.JobID == id                   
            select m).Distinct().AsEnumerable<Material>();
}

主要的想法是,如果我获得了type和id值,我应该使用 JobID == id 获取作业表中未提及的所有用户,如果他们有 city == type 。现在,它返回提到和不提取,如果我删除 Distinct() ,它会返回许多重复项。有谁知道如何解决这个问题?谢谢!

解决:

谢谢你们!!!我找到了答案,这段代码实际上已经开始工作了: C#:

public IEnumerable<Material> GetAllMaterialsByTypeNotSelected(string type , int id)
        {
            return (from m in dataContext.Person
                   where !(from o in dataContext.Job
                          where o.JobID == id
                          select o.PersonID).Contains(m.Id)&& 
                   m.City == type                  
                   select m).Distinct().AsEnumerable<Material>();
}

3 个答案:

答案 0 :(得分:2)

我改变了返回类型,如果我理解正确,你想带那些没有工作的人。

public IEnumerable<Person> GetAllMaterialsByTypeNotSelected(string type , int id)
{
 return  dataContext.Person
 .Where(p => dataContext.Job.FirstOrDefault(j => j.PersonId == p.PersonId)== null);
}

答案 1 :(得分:1)

var result = from person in ( from p in dataContext.Persons
                              where string.Compare( p.City, type, true ) == 0
                              select p )

             join job in ( from j in dataContext.Jobs
                           where j.JobID == id 
                           select j )
               on person.id equals job.PersonID
             into jobJoinData
             from jobJoinRecord in jobJoinData.DefaultIfEmpty( )

             where jobJoinRecord == null 

             select person;

我不确定您需要什么,但此查询会为您提供所有居住在指定城市(类型)并且没有给定作业(ID)的Persons

答案 2 :(得分:1)

以下是我在示例项目中构建的相同案例。

 class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

class Pet
    {
        public string Name { get; set; }
        public Person Owner { get; set; }
    }

static void Main(string[] args)
        {
            Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
            Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
            Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
            Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };

            Pet barley = new Pet { Name = "Barley", Owner = terry };
            Pet boots = new Pet { Name = "Boots", Owner = terry };
            Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
            Pet bluemoon = new Pet { Name = "Blue Moon", Owner = terry };
            Pet daisy = new Pet { Name = "Daisy", Owner = magnus };

            // Create two lists.
            List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
            List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };

            var query = from person in people
                        join pet in pets on person equals pet.Owner into gj
                        from subpet in gj.DefaultIfEmpty() where subpet == null
                        select new { person.FirstName};

            foreach (var v in query)
            {
                Console.WriteLine(v.FirstName );
            }
        }

这将简单地将arlene打印到控制台中,而宠物收藏中不存在该控制台。