简体   繁体   English

Linq查询不返回任何东西

[英]Linq query not returning anything

So, I have a SQL query that works like this, 因此,我有一个像这样的SQL查询,

SELECT *
FROM db.history a
inner join db.history b 
on a.image_id = image_id 
and b.action in(200,201,202)  
where a.user_id = 'someuser'
and a.action in (104) 
and a.time_stamp between '2015-06-22 00:00:00' and '2015-06-22 23:59:59'

Now, I am trying to implement the same query in Linq in different sections like this: 现在,我试图在Linq的不同部分中实现相同的查询,如下所示:

1) First I get everything from the history table where the action = 103 and 104 and that is between the date range: 1)首先,我从history表中获得了所有内容,其中action = 103 and 104 ,并且介于日期范围之间:

var history = Db.Histories
                          .Select(i => i)   
                          .Where(i
                                  =>
                                     && i.TimeStamp >= startDate && i.TimeStamp <= endDate               
                                     && (i.ActionId == 103 || i.ActionId == 104 ))                                                           
                          .OrderBy(i => i.UserId)                                                       
                          .ToArray();

2) I get all the users: 2)我得到了所有用户:

var users = history.Select(i => i.UserId).Distinct().ToList();

These two queries are working fine. 这两个查询工作正常。 However, when I try to loop through all the users to implement the SQL query is not returning anything: 但是,当我尝试遍历所有用户以实现SQL查询时,未返回任何内容:

for(var user in users)
{
    var query = from h in history
                join h2 in Db.Histories.Where(i => i.Action == 200 || i.Action == 201 || i.Action == 202)
                on h.ImageId equals h2.ImageId
                where h.UserId.Equals(user) && h.Action == 104
                select h;
}

Why is empty? 为什么是空的?

Thank you 谢谢

You are filtering h to only have elements h.Action == 104 and h2 to only have i.Action == 200 || i.Action == 201 || i.Action == 202 您正在过滤h以仅包含元素h.Action == 104而h2仅包含i.Action == 200 || i.Action == 201 || i.Action == 202 i.Action == 200 || i.Action == 201 || i.Action == 202 i.Action == 200 || i.Action == 201 || i.Action == 202 which is a completely different set of results with none of the elements of h. i.Action == 200 || i.Action == 201 || i.Action == 202 ,这是完全不同的结果集,没有h的任何元素。

If ImageId is your primary key(or any other unique field) joining 2 different sets of result will always result in empty set 如果ImageId是您的主键(或任何其他唯一字段),则连接2套不同的结果集将始终导致空集

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM