Home > Net >  Excuse me, can dynamically create the List (of t) select () expression expression
Excuse me, can dynamically create the List (of t) select () expression expression

Time:09-17

Excuse me, can dynamically create the List (of t) select () expression expression
Namely: dim result=list (of t). Select ( function (p) {p.n ame, p.age, p. ex} ). Tolist
Red font can use variables? Function (p) p.n ame is can use, but you don't use an array

CodePudding user response:

You need to use the reflection

CodePudding user response:

Possible, although I have not played with vb.net
But something fundamental is consistent,

I use c # code, demonstrate,
Focused on the use or for Func entrust,

CodePudding user response:

If a lot of places to use, it can change the Test into generics

CodePudding user response:

refer to the third floor is nu month god reply:
if in many places, that can change the Test to the generic

Thank you for your reply, beginners to see don't quite understand,
What how can the selector to get?
My ultimate goal is to want to from a class that is a lot of fields according to the need to extract a few columns of data in the Datatable displayed

CodePudding user response:

reference 4 floor MJZXLMG response:
Quote: refer to the third floor is nu month god reply:

If a lot of places to use, it can put the Test into a generic

Thank you for your reply, beginners to see don't quite understand,
What how can the selector to get?
My ultimate goal is to want to from a class that is a lot of fields according to the need to extract a few columns of data in the Datatable displayed

The selector ninety your function (p)
But need a delegate to encapsulate,
Specific vb.net grammar I don't know, but there must be,

CodePudding user response:

reference 5 floor is nu month god reply:
Quote: refer to 4th floor MJZXLMG response:

Quote: refer to the third floor is nu month god reply:

If a lot of places to use, it can put the Test into a generic

Thank you for your reply, beginners to see don't quite understand,
What how can the selector to get?
My ultimate goal is to want to from a class that is a lot of fields according to the need to extract a few columns of data in the Datatable displayed

The selector ninety your function (p)
But need a delegate to encapsulate,
Specific vb.net grammar I don't know, but there must be,


Can be written in c # example, I can be converted into VB.NET syntax
Found online, but I don't want to come out all of the fields, a few fields
The Public Function CreateSelecter (Of TSource, TResult) (ByVal fieldNames () As String) As Expression (Of Func (Of TSource, TResult))
Dim param As ParameterExpression=Expression. The Parameter (GetType (TSource), "p")
Dim NewTReslut=Expression. [New] (GetType (TResult))
Dim bindingList As New List (Of MemberBinding)
For Each fieldName As String In fieldNames
Dim Propertyinfo As Propertyinfo=GetType (TSource) GetProperty (fieldName)
Dim Expr As MemberExpression=Expression. PropertyOrField (param, fieldName)
Dim m As MemberAssignment=Expression. The Bind (Propertyinfo, Expr)
BindingList. Add (m)
Next
Dim MemberInit As MemberInitExpression=Expression. MemberInit (NewTReslut bindingList)
Dim the selector As Expression (Of Func (Of TSource, TResult))=CType (Expression. Lambda (MemberInit, param), Expression (Of Func (Of TSource, TResult)))
Return the selector
End Function

CodePudding user response:

http://www.chinaoc.com.cn/p/1303571.html

CodePudding user response:

refer to 6th floor MJZXLMG response:

Or you wait is predestined friends the human, help you to translate the above my c # code,
I do not understand the vb.net, to write too hard,

CodePudding user response:

refer to the eighth floor is nu month god reply:
Quote: refer to the sixth floor MJZXLMG response:

Or you wait is predestined friends the human, help you to translate the above my c # code,
I do not understand the vb.net, to write too hard,


You can read the code above, most other key lies in how to dynamically build the selector expressions,
I posted the code reference is a c # code: http://www.tnblog.net/aojiancc2/article/details/2346
Can be dynamically generated the SELECTOR expressions, but need an entity class TResult, so the question comes, how I've created in code entity class why dynamically create TResult me, directly to the new one, what I want is a few fields in the original entity class, don't TResult.
You reply in the https://bbs.csdn.net/topics/392403424 this post is also a lack of complete case, I am stupid don't understand, can you to write a simple c # code examples, thank you.

CodePudding user response:

references 9 f MJZXLMG response:
you can read the code above, most other key lies in how to dynamically build the selector expressions,
I posted the code reference is a c # code: http://www.tnblog.net/aojiancc2/article/details/2346
Can be dynamically generated the SELECTOR expressions, but need an entity class TResult, so the question comes, how I've created in code entity class why dynamically create TResult me, directly to the new one, what I want is a few fields in the original entity class, don't TResult.
You reply in the https://bbs.csdn.net/topics/392403424 this post is also a lack of complete case, I am stupid don't understand, can you to write a simple c # code examples, thank you,


 public static void Main (string [] args) 
{
Var q=Test (x=& gt; New User {ID=x.I D, Name=x.N ame});

Console.ReadLine();
}

Public static IEnumerable The Test (Func The selector)
{
Return myList. Select (the selector);
}

Public class User
{
Public int ID {get; set; }
Public string Name {get; set; }
Public int the Age {get; set; }
Public bool? IsChild {get; set; }

}


CodePudding user response:

Simply translate the moderator code of adult, but I think did not GET to the meaning of the original poster.

 
Public Class student
Public name As String
Public sex As String
Public age As Integer
The End of the Class

Public class two years As New List (Of student)

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related