2017-01-23 12 views
0

"LEFT JOIN"を作成しようとしています。左結合エンティティフレームワークが実行されていません

しかし、「両方のテーブルに」という情報があるときだけ、コマンドは何かを返します。私はこれを行う必要があり、私の機能

public IList<ClientWorkFlowReadModel> GetWorkFlow(int idClient) 
       { 
        try 
        { 
         using (GvisaContext db = new GvisaContext()) 
         { 
          //Disable de proxy tracking for prevent error in json convertion 
          db.Configuration.ProxyCreationEnabled = false; 

          var clientServiceWorkFlows = db.ServicesWorkFlow 
           .Join(db.ClientsServicesWorkFlow, 
             swf => swf.IdServiceWorkFlow, 
             cswf => cswf.IdServiceWorkFlow, 
             (swf, cswf) => 
           new { swf, cswf }) 
           .Select(x => new ClientWorkFlowReadModel { 
            Title = x.swf.Title, 
            IdClient = x.cswf.IdClient, 
            IdService = x.swf.IdService, 
            IdClientServiceWorkFlow = x.cswf.IdClientServiceWorkFlow, 
            Description = x.swf.Description, 
            Active = x.swf.Active, 
            DateUpdate = x.cswf.DateUpdate 
            } 
           ).Where(y => y.IdClient == idClient).ToList(); 

          return clientServiceWorkFlows; 
         } 
        } 
        catch (Exception ex) 
        { 
         throw ex; 
        } 

ベロー

SELECT * FROM dbo.Clients z 
INNER JOIN dbo.ClientsServices y on y.idClient=z.idClient 
INNER JOIN dbo.ServicesWorkFlow a on a.IdService=y.IdService 
LEFT JOIN ClientsServicesWorkFlow b on b.IdClientServiceWorkFlow=a.IdServiceWorkFlow 
WHERE z.IdClient=3 

感謝を!

+0

を使用してエンティティに参加し、[LINQの者の参加は使用しないでください。ナビゲート!](https://coding.abel.nu/2012/06/dont-use-linqs-join-navigate/) –

答えて

1
 var clientServiceWorkFlows = (from z in db.Clients 
    join y in db.ClientsServices on z.idClient equals y.idClient 
    join a in db.ServicesWorkFlow on y.IdService equals a.IdService 
    join ClientsServicesWorkFlow b on a.IdServiceWorkFlow equals b.IdClientServiceWorkFlow into g 
    from x in g.DefaultIfEmpty()).Select([... what you want] 
           ).Where(y => y.IdClient == idClient).ToList(); 

上記を試してください。それが動作するかどうか私に教えてください。 EFで作業する場合左用

リファレンスDefaultIfEmpty、Entity framework left join

+0

DefaultIfEmptyにエラーがあります –

+0

@AlexPereiraMahmudどのエラー? linqをインポートしましたか? –

+0

私はいつも「クエリ本体はselect節または節グループで終わらなければならない」というエラーを受けています –

関連する問題