Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq查询未给出所需结果_C#_Linq_Join_Asp.net Core - Fatal编程技术网

C# Linq查询未给出所需结果

C# Linq查询未给出所需结果,c#,linq,join,asp.net-core,C#,Linq,Join,Asp.net Core,我正在构建一个linq查询,它提供一个最小值。但是,我一直得到错误的最小值。我肯定这是一个加入问题,但我不能得到一个外部加入工作。我尝试了一些基于其他帖子的外部连接,但没有任何效果。我不确定需要连接哪个表才能使此工作正常。我想我已经试过了我想我能做的每一个组合 以下是我开始的查询: (from r in Results join entry in Entries on r.EntryId equals entry.EntryId join entryEvent in EntryE

我正在构建一个linq查询,它提供一个最小值。但是,我一直得到错误的最小值。我肯定这是一个加入问题,但我不能得到一个外部加入工作。我尝试了一些基于其他帖子的外部连接,但没有任何效果。我不确定需要连接哪个表才能使此工作正常。我想我已经试过了我想我能做的每一个组合

以下是我开始的查询:

(from r in Results
    join entry in Entries on r.EntryId equals entry.EntryId
    join entryEvent in EntryEvents on entry.EntryId equals entryEvent.EntryId
    join eswimmer in EntrySwimmers on entry.EntryId equals eswimmer.EntryId
    where eswimmer.SwimmerId == 12027 && entryEvent.EventNumberId == 1233
    select r.Time)
.Min();
外部联接尝试:

from r in Results
join en in Entries on r.EntryId equals en.EntryId
join ev in EntryEvents on en.EntryId equals ev.EntryId into evJoined
join s in EntrySwimmers on en.EntryId equals s.EntryId into sJoined
from ev in evJoined.DefaultIfEmpty()
from s in sJoined.DefaultIfEmpty()
where (s.SwimmerId == 12027 && ev.EventNumberId == 1233)
select r.Time;
问题是我从游泳运动员的所有结果中得到的是最小值,而不是该运动员和特定项目的最小值

类结构如下所示:

结果

public int ResultId
public int EntryId
public ICollection<Entry> Entry
public int EntryEventId
public ICollection<EntryEvent> EntryEvent
public TimeSpan Time
入口游泳者

public int EntryId
public Entry Entries
public int SwimmerId
public Swimmer Swimmers

我仍在学习linq、JOIN等,因此非常感谢您的解释。提前谢谢你

您已经映射了导航属性,因此基本上不需要任何连接,因为ORM将为您提供:

var min = Results
            .Include(x => x.Entry.Select(y => y.EntrySwimmers)) //here you should rename Entry to Entries ideally in your result class  
            .Include(x => x.Entry.Select(y => y.EntryEvents))
            .Where(x => x.Entry.EntrySwimmers.Any(y => y.SwimmerId == 12027) && x.EntryEvents.Any(y => y.EventNumberId == 1233))              
            .Min(x => x.Time);

不知道为什么结果和条目中都有事件。

先用T-SQL编写一个工作查询,然后再从那里向后工作。我在SQL中得到了相同的结果。这个模型对吗
Entry
有许多
EntryEvents
和许多
entryswimbers
。您将如何将游泳运动员链接到特定的
EntryEvent
?是的,因为一个条目可以是针对注册多个项目的个人的,也可以是包含一个项目和多个游泳运动员的中继条目。我尝试过使用导航属性,但由于某些原因,在这种情况下使用它们似乎无法获得结果。我最终使用了一个连接,从EntrySwarmer开始,它似乎起作用了。不过,我会继续尝试导航属性,因为我知道我必须在不久的将来为这个查询添加更多内容(比如只过滤本季过去的会议结果)。我已尝试了您所做的,但不断收到一条消息,即ICollection不包含EntrySwimmers的定义,而Result不包含EntryEvents的定义。然后,您应该检查映射以查看错误。您必须检查映射是否完全设置为DB,例如一对一、一对多关系我将where子句修改为:
where(x=>x.Entries.Any(y=>y.EntrySwimmers.Any(e=>e.SwimmerId==SwimmerId))&&x.EntryEvent.Any(y=>y.EventNumberId==eventid))
但我只得到00:00:00.00作为输出。我检查了映射,它们看起来很好。我应该具体寻找什么?我认为您应该检查代码生成的sql,看看哪里出了问题?也许您应该在条目上使用entryevent,而不是结果
public int EntryId
public Entry Entries
public int SwimmerId
public Swimmer Swimmers
var min = Results
            .Include(x => x.Entry.Select(y => y.EntrySwimmers)) //here you should rename Entry to Entries ideally in your result class  
            .Include(x => x.Entry.Select(y => y.EntryEvents))
            .Where(x => x.Entry.EntrySwimmers.Any(y => y.SwimmerId == 12027) && x.EntryEvents.Any(y => y.EventNumberId == 1233))              
            .Min(x => x.Time);