Home > Net >  VB.net, how to dynamically build Linq Where clause
VB.net, how to dynamically build Linq Where clause

Time:09-30

Do a general program, because the keys and values are uncertain, how to write complete Linq statement?
Please use VB.Net, Linq to answer, thank you! Please as soon as possible!

CodePudding user response:

Try the Dynamic Linq:
https://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library

CodePudding user response:

Linq code hinting strongly typed when programming convenient, if is Dynamic, and splicing SQL statements like that makes no difference, if it is to return to the entity object, in this way also can write SQL directly returns the entity, and the Dynamic linq also no big difference

CodePudding user response:

https://download.csdn.net/download/caozhy/12285485
.
 Imports System. Linq Expressions 
Imports System. Linq
Imports System. Collections. The Generic

Class A
Public Property Name As a String
Public Property ID As an Integer
Public Overrides the Function the ToString () As String
Return the ID & amp; ", "& amp; The Name
End the Function
The End of the Class

The Module Module1

The Function MyWhere (Of T) (ByVal data As IEnumerable (Of T), ByVal propname As String, ByVal value As Object) As IEnumerable (Of T)
Dim param=Expression. The Parameter (GetType (T), the "x")
Dim body=Expression. Equal (Expression. MakeMemberAccess (param, GetType (T). The GetProperty (propname)), the Expression. The Constant (value))
Dim lambdaexpr As Object=Expression. Lambda (body, param). The Compile ()
Return the data. The Where (CType (lambdaexpr, Func (Of T, Boolean)))
End the Function

Sub Main ()
Dim the list As New list (Of A)
List. The Add (New A () With {. ID=1,. Name="A"})
List. The Add (New A () With {, ID=2, Name="b"})
List. The Add (New A () With {, ID=3, Name="c"})
Console. WriteLine (MyWhere (list, "Name", "a"). The First ())
Console. WriteLine (MyWhere (list, "ID", 2). The First ())
End Sub

End the Module

CodePudding user response:

If it's a linq to SQL/ef, can use the following
.
 Imports System. Linq Expressions 
Imports System. Linq
Imports System. Collections. The Generic

Class A
Public Property Name As a String
Public Property ID As an Integer
Public Overrides the Function the ToString () As String
Return the ID & amp; ", "& amp; The Name
End the Function
The End of the Class

The Module Module1

The Function MyWhere (Of T) (ByVal data As IQueryable (Of T), ByVal propname As String, ByVal value As Object) As IQueryable (Of T)
Dim param=Expression. The Parameter (GetType (T), the "x")
Dim body=Expression. Equal (Expression. MakeMemberAccess (param, GetType (T). The GetProperty (propname)), the Expression. The Constant (value))
Dim lambdaexpr As Object=Expression. Lambda (body, param)
Return the data. The Where (CType (lambdaexpr, Expression (Of Func (Of T, Boolean))))
End the Function

Sub Main ()
Dim the list As New list (Of A)
List. The Add (New A () With {. ID=1,. Name="A"})
List. The Add (New A () With {, ID=2, Name="b"})
List. The Add (New A () With {, ID=3, Name="c"})
Console. WriteLine (MyWhere (list. AsQueryable (), "Name", "a"). The First ())
Console. WriteLine (MyWhere (list. AsQueryable (), "ID", 2). The First ())
End Sub

End the Module

CodePudding user response:

This watch is a great god, cow force,,,,

CodePudding user response:

Good logic and can immediately see that error when programming development, and have basic performance advantage, at least you write the code immediately after lexical analysis let machine can perceive what you wrote "key words", if you can't even write key words out, why should start to write Linq statement?

CodePudding user response:

 thank you, I have a try

CodePudding user response:

Answer the second floor, is that in VS dealt with some lines set inside, these lines set in different DataTable, and cannot use RowFilter find them in advance, need to take more than one table in the process by the primary key to compare, for example, to associate, so the Where clause can not directly determine in which field association,
The method of 3 buildings, I study again, is not very bright, if could explain the principle of slightly, forgive me is a first hand!
As for the 6th floor, no matter you will not, or do not say, doesn't make sense to everyone. You say,

CodePudding user response:

I have written a, annotation properties on the ViewModel, and then generate lambda expressions using the Entity attribute
Is c #, however, is not perfect, when the future
 
Public static class ExpressAction
{
Internal class QueryField
{
Public ViewModelLabelAttribute Label {get; set; }

Public PropertyInfo Property {get; set; }

Public object Value {get; set; }

Internal Expression HandleField (ParameterExpression parameter)
{
If (Value=null https://bbs.csdn.net/topics/=return Expression. The Empty ();
Var memberExpr=Expression. The Property (parameter, the Property);
The switch (parison Label.Com
{
Case ExpressComparison. Equal:
Return Expression. Equal (memberExpr Expression. Constant (Value));
Case ExpressComparison. Gt:
Return Expression. GreaterThan (memberExpr Expression. Constant (Value));
Case ExpressComparison. GtOrEqual:
Return Expression. GreaterThanOrEqual (memberExpr Expression. Constant (Value));
Case ExpressComparison. Lt:
Return Expression. LessThan (memberExpr Expression. Constant (Value));
Case ExpressComparison. LtOrEqual:
Return Expression. LessThanOrEqual (memberExpr Expression. Constant (Value));
Case ExpressComparison. NoEqual:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • LINQ