开发者

combining linq queries

开发者 https://www.devze.com 2022-12-13 16:58 出处:网络
i have a couple of linq queries which I wanted to join var IDs = from p in ctx.bam_Zending_AllInstances

i have a couple of linq queries which I wanted to join

var IDs = from p in ctx.bam_Zending_AllInstances
                          where p.Zender == "RT30"
                          select new { Identificatie = p.Identificatie };

                var latestIDs = from p in ctx.bam_Zending_AllInstances
                                where p.Zender == "RT30"
                                group p by p.Identificatie into q
                                select new { Identificatie = q.Key, Datum = q.Max(p => p.PrestatieOntvangen) };

                var final = from p in IDs
                            join q in latestIDs on p.Identificatie equals q.Identificatie
                            select new { Identificatie = p.Identificatie };

The problem is. If I look at the results, the IDs have 308 items, the latestIDs have 304 items, but the final one also has 308 items. How is this possible? I thought a join in linq was an in开发者_开发百科ner join. I need to have 304 items, but instead it does an outer join.

What am I doing wrong?


Are you sure you have unique IDs here? If you have two rows with the same ID, the join will generate four matches instead of two.

I suggest you look at the generated SQL for the final query - I would certainly expect it to be an inner join.


The ID wasn't unique indeed.

I fixed the problem by using two conditions to join:

var IDs = from p in ctx.bam_Zending_AllInstances
                          where p.Zender == "RT30"
                          select new { ID = p.Identificatie, Datum = p.PrestatieOntvangen };

                var latestIDs = from p in ctx.bam_Zending_AllInstances
                                group p by p.Identificatie into q
                                select new { ID = q.Key, Datum = q.Max(p => p.PrestatieOntvangen) };

                var final = from p in IDs
                            join q in latestIDs on new { p.ID, p.Datum } equals new { q.ID, q.Datum }
                            select new { ID = p.ID };
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号