Another way is to use query like this:
var qry = from c in Categories join
s in Subcategories.Select((a,x) => new{cateid = a.cateid, index=x+1, subcateid = a.subcateid, subcatename = a.subcatename})
on c.cateid equals s.cateid
select new
{
cateid = s.index==1 ? c.cateid.ToString() : string.Empty,
catename = s.index==1 ? c.catename : string.Empty,
subcateid = s.subcateid,
subcatename = s.subcatename
};
Result:
cateid catename subcateid subcatename
1 Modular Office Furniture 40 45mm Panel Base System
41 60mm Panel Base System
42 75mm Panel Base System
Complete sample code for
LinqPad[
^]:
void Main()
{
List<category>> Categories = new List<category>
{
new category(1, "Modular Office Furniture")
};
List<subcategory> Subcategories = new List<subcategory>
{
new subcategory(40, 1, "45mm Panel Base System"),
new subcategory(41, 1, "60mm Panel Base System"),
new subcategory(42, 1, "75mm Panel Base System")
};
var qry = from c in Categories join
s in Subcategories.Select((a,x) => new{cateid = a.cateid, index=x+1, subcateid = a.subcateid, subcatename = a.subcatename})
on c.cateid equals s.cateid
select new
{
cateid = s.index==1 ? c.cateid.ToString() : string.Empty,
catename = s.index==1 ? c.catename : string.Empty,
subcateid = s.subcateid,
subcatename = s.subcatename
};
qry.Dump();
}
class category
{
private int cid = 0;
private string cn = string.Empty;
public category(int _cid, string _cn)
{
cid = _cid;
cn = _cn;
}
public int cateid
{
get{return cid;}
set{cid = value;}
}
public string catename
{
get{return cn;}
set{cn = value;}
}
}
class subcategory
{
private string scn = string.Empty;
private int scid = 0;
private int cid = 0;
public subcategory(int _scid, int _cid, string _scn)
{
scid = _scid;
cid = _cid;
scn = _scn;
}
public int cateid
{
get{return cid;}
set{cid = value;}
}
public int subcateid
{
get{return scid;}
set{scid = value;}
}
public string subcatename
{
get{return scn;}
set{scn = value;}
}
}