none
Linq query does not return any result? RRS feed

  • Question

  • Hello,

    Following is the linq query which perform join in two tables but it is not return any rows while dataset's table having row with similar ids.

     

    var

     

    results = from table1 in

    dsSurveyMaster.Tables[0].AsEnumerable ()

     

    join table2 in

    dsParentSurvey.Tables[0].AsEnumerable()

     

    on table1["sur_SurveyId"

    ]

     

    equals table2["SurveyID"

    ]

     

    where (DateTime)table2["date"] >= dStartDate && (DateTime)table2["date"

    ] <= dEndDate

     

    select

    new

    {

    SurveyName = (

    string)table1["sur_SurveyName"

    ],

    SurveyId = (

    int)table2["SurveyID"

    ],

    SurveyDate = (

    DateTime)table2["Date"

    ],

    SurveyIdtemp = (

    int)table2["Survey_Id"

    ],

    PlayerId = (

    string)table2["PlayerId"

    ]

    };

    Please help me to get away this problem.

    Any Help is really appreciatable.

    Thanks in advance.

    • Moved by Aspen VJ Friday, May 6, 2011 2:02 AM (From:Visual C# General)
    Wednesday, May 4, 2011 1:45 PM

Answers

  • Usually casting to an int from a data table doesn't work because columns can be nullable and ints cannot have null values.  That's why nullable types were introduced to .NET.  You might have more success casting your column values as int? instead of int if you're positive that they're ints.  You could also try the Convert class (Convert.ToInt32).

    I would focus on making sure the columns you are accessing in each table are the types you expect, cast them appropriately, and make sure object.Equals(table1column, table2column) returns what you expect it to for equal and unequal values.  Then, eliminate your where clause to make sure that's not the problem, then reintroduce the where to make sure it's not incorrectly filtering your results.

    Evan

    Wednesday, May 4, 2011 5:07 PM
  • have you tried this :

    on table1["sur_SurveyId"].ToString() equals table2["SurveyID"] .ToString()
    

    or something like

    on table1["sur_SurveyId"].Value equals table2["SurveyID"] .Value
    
    

    ?

    Table1["sur_SurveyId"] is something of type DataRow. Your first DataRow is not equal to the second one. You should compare the values of the datarows instead.

     

    Wednesday, May 11, 2011 3:21 PM

All replies

  • Hi,

    Seems to me the error is in the join. The equals will use a reference equality between the two column values, since table["columnName"] returns an object. And the object will most likely not be the same object, so the equals fails. Instead of the join you could use a SelectMany. The code could also be written like this:

     

    var results = from table1 in dsSurveyMaster.Tables[0].AsEnumerable () 
        from table2 in dsParentSurvey.Tables[0].AsEnumerable() 
    where (string)table1["sur_SurveyId"] == (string)table2["SurveyID"] &&
      (DateTime)table2["date"] >= dStartDate && (DateTime)table2["date"] <= dEndDate 
    select new {
     SurveyName = (string)table1["sur_SurveyName"], 
     SurveyId = (int)table2["SurveyID"], 
     SurveyDate = (DateTime)table2["Date"], 
     SurveyIdtemp = (int)table2["Survey_Id"], 
     PlayerId = (string)table2["PlayerId"]
    };
    
    

     

    I have not tried the code, since I have no valid dataset...


    Hope this helps
    Meile Zetstra


    http://blogger.xs4all.nl/mzetstra

     


    Wednesday, May 4, 2011 3:26 PM
  • Thanks for the advice,

    But this code fails to work for me.It also gives datatable having zero number of rows.

    and when do the casting under the where clause on condition check like((int)table1["sur_surveyID"]) ,it gives the exception that casting is not allowed.

    Any way thanks,

    I am still searching for the right code. 

    Wednesday, May 4, 2011 4:56 PM
  • Usually casting to an int from a data table doesn't work because columns can be nullable and ints cannot have null values.  That's why nullable types were introduced to .NET.  You might have more success casting your column values as int? instead of int if you're positive that they're ints.  You could also try the Convert class (Convert.ToInt32).

    I would focus on making sure the columns you are accessing in each table are the types you expect, cast them appropriately, and make sure object.Equals(table1column, table2column) returns what you expect it to for equal and unequal values.  Then, eliminate your where clause to make sure that's not the problem, then reintroduce the where to make sure it's not incorrectly filtering your results.

    Evan

    Wednesday, May 4, 2011 5:07 PM
  • Check to make sure that all of your dictionary keys are correct. 
    Sunday, May 8, 2011 3:27 AM
  • have you tried this :

    on table1["sur_SurveyId"].ToString() equals table2["SurveyID"] .ToString()
    

    or something like

    on table1["sur_SurveyId"].Value equals table2["SurveyID"] .Value
    
    

    ?

    Table1["sur_SurveyId"] is something of type DataRow. Your first DataRow is not equal to the second one. You should compare the values of the datarows instead.

     

    Wednesday, May 11, 2011 3:21 PM