![](/img/trans.png)
[英]Linq query doesn't return anything even if DateTime values are the same
[英]Linq query doesn't return correct result if values are in different tables
您好:我有兩個桌子。
表格:
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來獲得Job Tables中未提及的所有用戶,並且如果他們具有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>();
}
我會更改退貨類型,如果我理解正確,那么您想雇用沒有工作的人員。
public IEnumerable<Person> GetAllMaterialsByTypeNotSelected(string type , int id)
{
return dataContext.Person
.Where(p => dataContext.Job.FirstOrDefault(j => j.PersonId == p.PersonId)== null);
}
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;
我不太確定你所需要的,但此查詢會給你的所有Persons
,其居住在給定的市(型),不具有給定的工作(ID)。
這是我在示例項目中構建的相同案例。
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打印到Pets集合中不存在的控制台。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.