English 中文(简体)
有条件加入的班子
原标题:Linq with conditional joins

I couldn t convert the following left join SQL to linq:

select  Students.StudentID, StudentAddresses.state
from Students left join Studentaddresses on (Students.StudentID = Studentaddresses.StudentID and StudentAddresses.Active=1)
where (StudentAddresses.Rank =1  or StudentAddresses.StudentID is null)
and Students.StudentID =3

学生可以在学生演讲桌上拥有零记录或多份记录,但只有一份记录可以活跃,排名一。

我能够做一个左边会,为正常局势工作。 但是,如果学生在学生桌上有两张不活跃的记录,我不知道如何使学生记录在最后结果中只出现过一次。 谁能提供帮助?

问题回答

使用<代码>代号()

var LeftJoin = (from student in Students
                join address in (from address1 in StudentAddresses where address.Active select address1) 
                on student.StudentID equals address.StudentId
                into JoinedStudentAddress
                from joined in JoinedStudentAddress.DefaultIfEmpty()
                select new                          
                {
                    StudentID = student.StudentID,
                    State = joined != null ? joined.State : null
                }).Distinct();

Alternate syntax

 var LeftJoin = Students.GroupJoin( StudentAddress.Where( a => a.Active ),
                                    s => s.StudentID,
                                    a => a.StudentID,
                                    (s,a) => new { Student = s, Addresses = a } )
                        .SelectMany( j = > j.Addresses.DefaultIfEmpty()
                                     (s,a) => new {
                                                 StudentID = s.Student.StudentID,
                                                 State = a != null ? a.State : null
                                    })
                        .Distinct();

This would be the SQL statement converted to linq:

var q = from student in Students
        from adress in Studentaddresses.Where(x => student.StudentID == x.StudentID && x.Active).DefaultIfEmpty()
        where (adress.Rank == 1 || adress.StudentID == null) && student.StudentID == 3
        select new                          
        {
           StudentID = student.StudentID,
           State = adress.state
        };




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签