1

I have Product table and Officer table as follow:

Product

ProductID | ProductName | Officer1ID | Officer2ID | Officer3ID
--------- | ----------- | ---------- | ---------- | ----------
12        | Mouse       | 123        | 124        | 125
13        | Keyboard    | 234        | 235        | 0

Officer

OfficerID | OfficerName 
--------- | ----------- 
123       | John       
124       | Andy    
125       | Mark



I need to join 3 columns (Officer1ID, Officer2ID, Officer3ID) from Product table with OfficerID in Officer table to produce result like this:

ProductID | ProductName | Officer1Name | Officer2Name | Officer3Name
--------- | ----------- | ------------ | ------------ | ------------
12        | Mouse       | John         | Andy         | Mark
13        | Keyboard    | Dave         | Fred         | Leon



This is my attempt. I know how to join 1 field, but not multiple. Can anyone help? Thanks!

List<Product> lstProduct = GetProducts();

List<Officer> lstOfficer = GetOfficers();

var merge = from p in lstProduct
   join from o in lstOfficers on p.Officer1ID equals o.OfficerID
   select new { ProductID = p.ProductID, ProductName = p.ProductName, OfficerName = o.OfficerName };

EDIT
OfficerIDs in Product table could be 0(not exist in Officer table).

bla
  • 5,350
  • 3
  • 25
  • 26
  • 2
    Columns called `Foo1`, `Foo2`, etc... are generally a sign that your data model is wrong. – Mark Byers Oct 22 '10 at 08:00
  • @mark byers: some kind of relation-table should be supported instead (just to complete your comment) –  Oct 22 '10 at 08:01
  • Our client has various products. Different product will have different product officer, finance officer project officer assigned to take care of the product. This is the requirement. – bla Oct 22 '10 at 08:12

3 Answers3

4

Just apply the join 3 times (once for each OfficerID):

var merge = from p in lstProduct
            join o1 in lstOfficer on p.Officer1ID equals o1.OfficerID
            join o2 in lstOfficer on p.Officer2ID equals o2.OfficerID
            join o3 in lstOfficer on p.Officer3ID equals o3.OfficerID
            select new
                     {
                         ProductID = p.ProductID,
                         ProductName = p.ProductName,
                         Officer1Name = o1.OfficerName,
                         Officer2Name = o2.OfficerName,
                         Officer3Name = o3.OfficerName
                     };
Yakimych
  • 17,612
  • 7
  • 52
  • 69
  • Thanks for the answer. This is quite close to what I am looking for. I forgot to mention that some OfficerIDs in Product table are null/empty. – bla Oct 25 '10 at 02:04
3

You can do this using multiple joins.

You should reconsider your data model. I suggest using junction table to make many to many relationship:

Product

ProductID | ProductName | ProductOfficiersID
--------- | ----------- | ---------- 
12        | Mouse       | 1        
13        | Keyboard    | 2       

ProductOfficiers

ProductOfficiersID | ProductID | OficierId 
------------------ | --------- | -----------
1                  | 12        | 123     
1                  | 12        | 124  
1                  | 12        | 125
2                  | 13        | 234
...

Officer ...

Community
  • 1
  • 1
Branimir
  • 4,327
  • 1
  • 21
  • 33
0

I ended up using subquery, inspired from this thread.

var merge = from p in lstProduct
            select new
            {
                p.ProductID,
                p.ProductName,
                Officer1Name = (from o in lstOfficer
                               where o.OfficerID == p.Officer1ID
                               select o.OfficerName).FirstOrDefault(),
                Officer1Name = (from o in lstOfficer
                               where o.OfficerID == p.Officer2ID
                               select o.OfficerName).FirstOrDefault(),
                Officer2Name = (from o in lstOfficer
                               where o.OfficerID == p.Officer3ID
                               select o.OfficerName).FirstOrDefault()
            };

Thanks guys for your help!

Community
  • 1
  • 1
bla
  • 5,350
  • 3
  • 25
  • 26