Home > Net >  Ask: how to use the variable as a LINQ select new out of the field names?
Ask: how to use the variable as a LINQ select new out of the field names?

Time:12-21

Known:
1, to deal with what table column is unknown, so can't write column names;
2, will all the columns in the combobox datatable, according to the user to select the column accordingly;
3, will deal with good results in a new column, a new column name is original choose to modify the column name + "modified"

So I don't know how to implement the following effects:
Cases of table 1:
Name the amount
Zhang SAN 10
Li si 15

Cases of table 2:
Trade name for
Fan 60
The refrigerator 200

. There are many different table for different processing, such as the amount of the table a unified plus 10, unified price by 0.8 table 2, and so on,

String field 1=table 2. The columns [0]. Columnname;//or table, or from the interface values, methods, the meaning of word is a variable
String field 2=CBX target columns. SelectedItem. ToString ();
String field 3=2 + "modified";

Var query=the from p in dt. AsEnumerable ()
Select new
{
Field 1=p.F ield (the "goods") + "ABC",//"brand name" and "price" is not written in field 1, field 2 is to avoid you see on the left with confusion,
Field 2=p.F ield (" price "),//so separate write convenient see clear to express the meaning of
Field 3=p.F ield (" price ") * 0.8
};
//other code slightly

This is just a sample, has nothing to do with the actual effect, overall goal is to use in the select new variables as a field name, ask how to do?

CodePudding user response:

Write a code, you define a variable of type object, it refers to a "use the variable as a field name" object? This concept has never heard of c #, if you have "use the variable as a type name, code name" of the idea?

CodePudding user response:

In c #, there are several dictionary extension type, ExpandoObject, for example, it is the key of type string dictionary, such as
 dynamic x=new ExpandoObject (); 
X.a=100;
X.b="200";
Var y=(IDictionary) x;
Var u=(int) y (" x ");
Var v=(string) y [b];

Including Dictionary Type and so on, can be to achieve IDictionary Such as function, but want to know what it is called "as a field name," this is a dictionary, is the most basic data structures,
  •  Tags:  
  • LINQ
  • Related