How to make this SQL in LINQ to SQL?

2019-07-07 04:53发布

问题:

I'm familiar with the way SQL works but working with LINQ to SQL gives me an headache. I've the following query but I not able to change it to LINQ to SQL. Can someone help me with this query or explain me how to work this on out?

 SELECT
         DISTINCT Pr.Pcode_,
         Pr.Omschrijving,
         Pt.TypeOmschr 
    FROM 
        Projecten AS Pr
    INNER JOIN 
        ProjectTypen AS Pt 
        ON Pr.ProjType_ = Pt.TypeID 
        AND
        Pr.Status NOT IN ( 'Afgerond', 'Afgewezen' ) 
        LEFT OUTER JOIN 
        Personeel AS Pe
        ON Pr.SeniorId_ = Pe.PerId_ 
        OR 
        Pr.CorId_ = Pe.PerId_
        WHERE  
        ( Pe.Naam LIKE '%%' ) 
         OR  
        ( Pr.Omschrijving LIKE '%%' ) 
        OR
        ( Pr.Pcode_ LIKE '%%' ) 
        OR 
       ( Pt.TypeOmschr LIKE '%%' )
        ORDER  BY 
        Pr.Pcode_

Edit:
I have the following L2S but it is not working. I'm getting the following error: The name 'Pe' is not in scope on the left side of 'equals'. I guess the problem is the left join, but what is the solution?

    from Pr in _db.Projectens  
    join Pt in _db.ProjectTypens on Pr.ProjType_ equals Pt.TypeID  
    join Pe in _db.Personeels on ((Pr.SeniorId_ == Pe.PerId_) || (Pr.SeniorId_ == Pe.PerId_)) into P  
    where Pr.Pcode_.Contains(search) || Pr.Omschrijving.Contains(search) || Pt.TypeOmschr.Contains(search) || Pe.Naam.Contains(search)  
    select new SearchProjectViewModel {  
        ProjectCode = Pr.Pcode_,  
        ProjectName = Pr.Omschrijving,  
        ProjectType = Pt.TypeOmschr  
    };

回答1:

I think

join Pe in _db.Personeels on ((Pr.SeniorId_ == Pe.PerId_) || (Pr.SeniorId_ == Pe.PerId_)) into P

should be

join Pe in _db.Personeels on ((Pr.SeniorId_ equals Pe.PerId_) || (Pr.SeniorId_ equals Pe.PerId_)) into P

try and let me know, if this works.