locked
Linq - left outer join doesnt work RRS feed

  • Question

  • here's my code :

    var result = from table1 in ds.Tables["DataA"].AsEnumerable()
                                 join table2 in ds.Tables["DataB"].AsEnumerable()
                                 on table1["product"] equals table2["product2"] 
                                 into temp
                                 from table3 in temp.DefaultIfEmpty()
                                 select new
                                 {
                                     Vendor = table1["Vendor"],
                                     Product = table1["Product"],
                                     Status = table3["Status"]
    
                                 };
    
    DataTable finalTable = result.CopyToDataTable();
                    dGv.DataSource = finalTable;

    The error message I got is : "Object reference not set to an instance of an object.". It pops up when get to "Product = table1["Product"],

    "

    What do I do wrong ?

    Friday, January 24, 2014 12:02 PM

Answers

  • Hi BukajResalg;

    See if this corrects the issue. Change the Product = ... As shown below. 

    select new
    {
        Vendor = table1["Vendor"],
        Product = (table1["Product"] != null) ? table1["Product"] : "",
        Status = table3["Status"]
    };

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Proposed as answer by Fred Bao Monday, January 27, 2014 1:50 AM
    • Marked as answer by Fred Bao Monday, February 3, 2014 8:53 AM
    Friday, January 24, 2014 4:10 PM
  • Hello BukajResalg,

    >>Linq - left outer join doesnt work

    In actual, the reason for throwing the error is that left outer join works, in some records, the “table1["Product"]” returns null value, however, the “table1["Product"]” is an object reference, it cannot be null, that is why the error occurs. For solving it, Fernando Soto has given the solution.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Monday, February 3, 2014 8:53 AM
    Monday, January 27, 2014 1:56 AM

All replies

  • Hi BukajResalg;

    See if this corrects the issue. Change the Product = ... As shown below. 

    select new
    {
        Vendor = table1["Vendor"],
        Product = (table1["Product"] != null) ? table1["Product"] : "",
        Status = table3["Status"]
    };

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Proposed as answer by Fred Bao Monday, January 27, 2014 1:50 AM
    • Marked as answer by Fred Bao Monday, February 3, 2014 8:53 AM
    Friday, January 24, 2014 4:10 PM
  • Hello BukajResalg,

    >>Linq - left outer join doesnt work

    In actual, the reason for throwing the error is that left outer join works, in some records, the “table1["Product"]” returns null value, however, the “table1["Product"]” is an object reference, it cannot be null, that is why the error occurs. For solving it, Fernando Soto has given the solution.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Monday, February 3, 2014 8:53 AM
    Monday, January 27, 2014 1:56 AM