Click here to Skip to main content
15,034,454 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

How can we get data from two tables by using left outer join in LINQ.

Regards,
S.Inayat Basha.
Posted
Comments
Christian Graus 13-Jul-10 6:34am
   
The big question is always, why do you have to use LINQ ?
Arun Jacob 30-Jul-10 4:27am
   
If he's using LINQ to SQL in data access layer, he may need joins.

Sample code,

var result = from accountDetails in context.AccountDetails
             join payment in context.PaymentDetails
             on accountDetails.AccountID equals payment.AccountID into temp
             from resultDetails in temp.DefaultIfEmpty()
             select new { temp.PayeeName,temp.AccountType };
   
The best way would be to create a procedure in SQL and use that as the datasource
   
Comments
Magnus_ 24-Aug-10 11:10am
   
Reason for my vote of 1
No
Hai.. Inayat Basha

i follow this for leftouterjoin.. But i am not sure this is better way.

regards
sanal p.s
9846385010
cochin

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace varklinq
{
public class cJAB_MT_SHOP_REGN_LQ
{

string _BOB_UNIQUEID;
long? _BOB_LOCATIONID;
long _NBRANCH_SL;
long? _NREGION_SL;
string _BOB_DESCRIPTION;
long? _NCM_SL;
string _CNAME;

public cJAB_MT_SHOP_REGN_LQ(string vBOB_UQ_ID, long? vBOB_LOC_ID, long vNBRANCH, long? vNRGION, string vBOB_DESCRTN, long? vNCM_SL, string vCNAME)
{
this._BOB_UNIQUEID = vBOB_UQ_ID;
this._BOB_LOCATIONID = vBOB_LOC_ID;
this._NBRANCH_SL = vNBRANCH;
this._NREGION_SL = vNRGION;
this._BOB_DESCRIPTION = vBOB_DESCRTN;
this._NCM_SL = vNCM_SL;
this._CNAME = vCNAME;
}
public cJAB_MT_SHOP_REGN_LQ()
{
}
public string BOB_UNIQUEID1
{
get { return _BOB_UNIQUEID; }
set
{

this._BOB_UNIQUEID = value;
}
}
public long? BOB_LOCATIONID
{
get { return _BOB_LOCATIONID; }
set { this._BOB_LOCATIONID = value; }
}
public long NBRANCH_SL
{
get { return _NBRANCH_SL; }
set { this._NBRANCH_SL = value; }
}

public long? NREGION_SL
{
get { return _NREGION_SL; }
set { this._NREGION_SL = value; }
}
public string BOB_DESCRIPTION
{
get { return _BOB_DESCRIPTION; }
set { this._BOB_DESCRIPTION = value; }
}
public long? NCM_SL
{
get { return _NCM_SL; }
set { this._NCM_SL = value; }
}
public string CNAME
{
get { return _CNAME; }
set { this._CNAME = value; }
}



public IEnumerable<cJAB_MT_SHOP_REGN_LQ> getALL()
{
DC_ShopRegionDataContext db = new DC_ShopRegionDataContext();
IEnumerable<cJAB_MT_SHOP_REGN_LQ> de = null;
de = from RTA in db.BOB_LOCATION_MASTERs

join RFPS in db.AB_MT_SHOP_REGIONs on RTA.BOB_LOCATIONID equals RFPS.BOB_LOCATIONID into ps
from p in ps.DefaultIfEmpty()
join CMREF in db.CM_MT_REF_CODEs on p.NREGION_SL equals CMREF.NCM_SL into rf
from r in rf.DefaultIfEmpty()
// where RFPS.BOB_LOCATIONID==xLOCID
orderby RTA.BOB_DESCRIPTION
select new cJAB_MT_SHOP_REGN_LQ
(
p == null ? "" : p == null ? "" : p.BOB_UNIQUEID,
RTA == null ? 0 : RTA == null ? 0 : RTA.BOB_LOCATIONID,
r == null ? 0 : r == null ? 0 : r.NBRANCH_SL,
p == null ? 0 : p == null ? 0 : p.NREGION_SL,
RTA.BOB_DESCRIPTION,
r == null ? 0 : r == null ? 0 : r.NCM_SL,
r.CNAME

);
return de;
}
public IEnumerable<cJAB_MT_SHOP_REGN_LQ> getALLRegion(long xNREGION)
{
DC_ShopRegionDataContext db = new DC_ShopRegionDataContext();
IEnumerable<cJAB_MT_SHOP_REGN_LQ> de = null;

de = from RTA in db.BOB_LOCATION_MASTERs

join RFPS in db.AB_MT_SHOP_REGIONs on RTA.BOB_LOCATIONID equals RFPS.BOB_LOCATIONID into ps
from p in ps.DefaultIfEmpty()
join CMREF in db.CM_MT_REF_CODEs on p.NREGION_SL equals CMREF.NCM_SL into rf
from r in rf.DefaultIfEmpty()
where p.NREGION_SL ==xNREGION
orderby RTA.BOB_DESCRIPTION
select new cJAB_MT_SHOP_REGN_LQ
(
p == null ? "" : p == null ? "" : p.BOB_UNIQUEID,
RTA == null ? 0 : RTA == null ? 0 : RTA.BOB_LOCATIONID,
r == null ? 0 : r == null ? 0 : r.NBRANCH_SL,
p == null ? 0 : p == null ? 0 : p.NREGION_SL,
RTA.BOB_DESCRIPTION,
r == null ? 0 : r == null ? 0 : r.NCM_SL,
r.CNAME

);

return de;
}



}
}
   
v2
Comments
Arun Jacob 30-Jul-10 4:24am
   
Format the code, use pre tags
Magnus_ 24-Aug-10 11:10am
   
Reason for my vote of 1
complicated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900