none
Wrrrrrrrrrr .......... syntax for First( ?????????????? RRS feed

  • Question

  • I'm trying to code a generel simple method for looking up values in a datatable.

    I have this code so fare:

    1         private string LookupThisIn(string searchFor, string inColumn, DataSet_Source.PERSONERDataTable tbl, string returnValueFromColumn)  
    2         {  
    3 //            return tbl.First(tbl.inColumn.ColumnName => tbl.inColumn= searchFor).returnValueFromColumn.ToString();  
    4             return tbl.First().Fornavn.ToString();  
    5         }  
    6  
    and it works and return the value in the column "Fornavn" for the first row in the table BUT .....

    1) the value of "searchFor" is what I'm searching for in ..
    2) .. the column "inColumn" and I ..
    3) .. want to return the value in the column "returnValueFromColumn" from the row where "condition" 1) +2) is true

    What are the syntax to this ?
    Best regards KSor, Denmark
    Saturday, January 31, 2009 6:40 PM

All replies

  • I got a little longer - I can now decide witch column to return values from:
    1         private string LookupThisIn(string searchFor, string searchCol, DataSet_Source.PERSONERDataTable tbl, DataColumn returnValueFromColumn)   
    2         {  
    3             //            return tbl.First(tbl.FornavnColumn.ColumnName => tbl.FornavnColumn = searchFor);  
    4             if (returnValueFromColumn.DataType.Name=="String")  
    5             {  
    6                 return tbl.First().Field<string>(returnValueFromColumn);  
    7             }  
    8             else if (returnValueFromColumn.DataType.Name=="Decimal")  
    9             {  
    10                 return tbl.First().Field<decimal>(returnValueFromColumn).ToString();  
    11             }  
    12             else if (returnValueFromColumn.DataType.Name=="Byte")  
    13             {  
    14                 return tbl.First().Field<Byte>(returnValueFromColumn).ToString();  
    15             }  
    16             else if (returnValueFromColumn.DataType.Name=="Int16")  
    17             {  
    18                 return tbl.First().Field<Int16>(returnValueFromColumn).ToString();  
    19             }  
    20             else 
    21             {  
    22                 return "FEJL <<< !";  
    23             }  
    24         }  
    25  
    but I don't think I have to test for every datatype - I would say something like this would do:

    return tbl.First().Field<returnValueFromColumn.DataType>(returnValueFromColumn).ToString();

    for every datatype - but I get this when building:

    The type or namespace name 'returnValueFromColumn' could not be found (are you missing a using directive or an assembly reference?) 

    Any ideas to wipe out all those IF-ELSE's ?

    Best regards KSor, Denmark
    Sunday, February 1, 2009 8:24 AM
  •  
    1 private string LookupThisIn(string searchFor, string inColumn, DataSet_Source.PERSONERDataTable tbl, string returnValueFromColumn)  
    2 {
        return (from row in tbl.AsEnumerable() where (tbl[inColumn].ToString() == searchFor) select row).First()[inColumn].ToString();
    5 }  

    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to mark the replies as answers if they help.
    Monday, February 2, 2009 2:08 AM
    Moderator
  • Mark.Ashton - I think there is a few problems here because  the "inColumn" in the  "tbl[inColumn]" MUST be a integer AND it is supposed to be a ROW-index !

    Meanwhile I've change my method like this to handle DataColumn objects instead:

    1         private string LookupThisIn(string searchFor  
    2                                   , DataColumn searchCol  
    3                                   , DataSet_Source.PERSONERDataTable tbl  
    4                                   , DataColumn returnValueFromColumn)  
    5         {  
    6  
    7             return (from row in tbl.AsEnumerable()  
    8                     where (tbl[searchCol.Caption].ToString() == searchFor)  
    9                     select row  
    10                     ).First()[searchCol].ToString();  
    11  
    but it should be just like your proposal - the "searchFor.Caption" in fact is the "inColumn"-string from my last method decalration ... but unfortunately the construction is not correct.

    I was not aware of this LINQ-way "from-where-select" - I thought of some Lambda-like code in the  First( ..... here ......) !

    Any proposals ?
    Monday, February 2, 2009 1:42 PM
  • ?????????????????????

    Where are the replies from Hans Kesting and another one - sorry I didn't get your name !!!!!!!!!!!

    Unfortunately non of your proposals would work ;-((

    This one:

    return tbl.First(tbl.FornavnColumn.ColumnName=>tbl.FornavnColumn=searchFor).ToString();

    shows an error here ...............................................^................................................^

                                                                            ; expected                                     invalid expresion term ')'

    And this one:

    return tbl.First().Field<returnValueFromColumn.DataType(returnValueFromColumn);

    shows ...................................................................^^^^^^^:
    Non-invocable member 'System.Data.DataColumn.DataType

    Maybe "mission imposible" ????


    Best regards KSor, Denmark
    Wednesday, February 4, 2009 8:11 PM