Syntax of .First(bla bla bla) ????????????

K

KS

I'm trying to code a generel simple method for looking up values in a
datatable - it should work like this:

Search for "searchFor" in the column "searchCol" in table "tbl" and return
the value from the column "returnValueFromColumn" in the found row af tha
table.

I have this code so fare:

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 "No found !";
23 }
24 }
25
It works with "returnValueFromColumn"
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 ALL datatypes ... ONE return and no IF-ELSE - 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 how :

1) the syntax is for First() - something like First( xxxxx p => searchCol =
searchFor) so that I can search the row where column "searchCol" is like
"searchFor"?
2) to automatically find out the datatype for the Field() - something like
Field<returnValueFromColumn.DataType>

Best regards
KSor, Denmark
 
J

jp2msft

Why can't you modify this line:

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

to be like this?

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

That is, take the ToString() off.
 
H

Hans Kesting

KS formulated the question :
I'm trying to code a generel simple method for looking up values in a
datatable - it should work like this:

Search for "searchFor" in the column "searchCol" in table "tbl" and return
the value from the column "returnValueFromColumn" in the found row af tha
table.

I have this code so fare:

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 "No found !";
23 }
24 }
25
It works with "returnValueFromColumn"
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 ALL datatypes ... ONE return and no IF-ELSE - 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 how :

1) the syntax is for First() - something like First( xxxxx p => searchCol =
searchFor) so that I can search the row where column "searchCol" is like
"searchFor"?
2) to automatically find out the datatype for the Field() - something like
Field<returnValueFromColumn.DataType>

Best regards
KSor, Denmark


The
"tbl.First().Field<returnValueFromColumn.DataType>(returnValueFromColumn).ToString();"
will not work as the compiler needs the type (between the <>) at
compile time.

As you are returning the string-representation of the column-value,
can't you do that directly? That is, without first getting specifically
the "Byte" value and then doing a plain ".ToString()". Maybe
Field<Object>(returnValueFromColumn) would work.

Would "return tbl.First(tbl.FornavnColumn.ColumnName =>
tbl.FornavnColumn = searchFor).ToString()" work? Or use a
FirstOrDefault.

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top