call ToArray on LINQ select query - vb.net

I have this query:
Dim test = result.GroupBy(Function(row) groupedindexes.Select(
Function(grpindex) row(grpindex)).ToArray, comp)
I'm building an expression tree. I have already build the part inside the GroupBy function and now I would like to call the ToArray method. Here is the code:
Public Function Grouping(ByVal result As IEnumerable(Of Object()), ByVal groupedindexes As List(Of Integer), ByVal comparer As compare) As Expression
Dim groupbyMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "GroupBy").MakeGenericMethod(GetType(Object()), GetType(System.Collections.Generic.IEqualityComparer(Of Object())))
Dim convertMethod As MethodInfo = Nothing
Dim rowParameter = Expression.Parameter(GetType(Object()), "Row")
Dim indexParameter = Expression.Parameter(GetType(Integer), "grpindex")
Dim methodstring As String
Dim expr As Expression = Nothing
Dim index As Integer
Dim grpindexes As Expression = Expression.Constant(groupedindexes, GetType(System.Collections.Generic.List(Of Integer)))
Dim selectMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "Select").MakeGenericMethod(GetType(Integer), GetType(Object))
Dim toarrayMethod2 = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "ToArray").MakeGenericMethod(GetType(Object))
Dim cmp As Expression = Expression.Constant(comparer, GetType(compare))
Dim fieldselector As Expressions.LambdaExpression
fieldselector = Expression.Lambda(Expression.ArrayAccess(rowParameter, indexParameter), indexParameter)
Dim outerfieldselector As Expressions.LambdaExpression
outerfieldselector = Expression.Lambda(Expression.Call(selectMethod, grpindexes, fieldselector), rowParameter)
expr = Expression.Call(groupbyMethod, Expression.Call(outerfieldselector, toarrayMethod2), cmp)
Return expr
End Function
I get an error message at the line expr = ...: Static method requires null instance, non-static method requires non-null instance.
I already know this error message, but I think, the expression call is correct: I call the ToArray method on outerfieldselector.
Could you help me out? You can copy&paste the code and test it. You can remove the compare class, if necessary.
Thanks.

You have a couple of mistakes:
You're getting the wrong GroupBy method.
Calls to static methods methods like GroupBy and ToArray need a null instance, just like the error says.
Here's the corrected code. Lines changed are the lines that begin with expr = and outerfieldselector = and getting the GroupBy method:
Public Function Grouping(ByVal result As IEnumerable(Of Object()), ByVal groupedindexes As List(Of Integer), ByVal comparer As compare) As Expression
Dim groupbyMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).
Where(Function(m) m.Name = "GroupBy").
First(Function(m) m.GetParameters().Count() = 3).
MakeGenericMethod(GetType(Object()), GetType(Object()))
Dim convertMethod As MethodInfo = Nothing
Dim rowParameter = Expression.Parameter(GetType(Object()), "Row")
Dim indexParameter = Expression.Parameter(GetType(Integer), "grpindex")
Dim methodstring As String
Dim expr As Expression = Nothing
Dim index As Integer
Dim grpindexes As Expression = Expression.Constant(groupedindexes, GetType(System.Collections.Generic.List(Of Integer)))
Dim selectMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "Select").MakeGenericMethod(GetType(Integer), GetType(Object))
Dim toarrayMethod2 = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "ToArray").MakeGenericMethod(GetType(Object))
Dim cmp As Expression = Expression.Constant(comparer, GetType(compare))
Dim fieldselector As Expressions.LambdaExpression
fieldselector = Expression.Lambda(Expression.ArrayAccess(rowParameter, indexParameter), indexParameter)
Dim outerfieldselector As Expressions.LambdaExpression
outerfieldselector = Expression.Lambda(Expression.Call(Nothing, toarrayMethod2, Expression.Call(selectMethod, grpindexes, fieldselector)), rowParameter)
Dim test = Enumerable.GroupBy(result, Function(row) groupedindexes.Select(Function(grpindex) row(grpindex)).ToArray(), comparer)
expr = Expression.Call(Nothing, groupbyMethod, Expression.Constant(result), outerfieldselector, Expression.Constant(comparer))
Return expr
End Function

Related

How to pass dynamically created type to function?

I am trying to get data from SQL query and convert to List of objects.
From SQL each time I get dataset with variable number of columns and different names of columns.
So I am trying to write some universal code to get and convert this dataset.
Dim MySqlStr As String
MySqlStr = "exec someStoredProcedure "
Try
MyAdapter = New SqlClient.SqlDataAdapter(MySqlStr, Declarations.MyNETConnStr)
MyAdapter.SelectCommand.CommandTimeout = 600
MyAdapter.Fill(MyDs)
'-----Dictionary of columns with types
Dim MyDict = New Dictionary(Of String, Type)
For i As Integer = 0 To MyDs.Tables(0).Columns.Count - 1
MyDict.Add(MyDs.Tables(0).Columns(i).ToString, MyDs.Tables(0).Columns(i).DataType)
Next
'----Dynamic type
Dim MyCls As Type
MyCls = CreateClass("DynClass", MyDict)
Dim MyObj As Object = Activator.CreateInstance(MyCls)
'-----Use newtonsoft
Dim json As String = JsonConvert.SerializeObject(MyDs, Formatting.Indented)
'-----here is a problem
'-----I am tryin to deserialize json
'-----but get a message that MyCls is not defined
'-----how to pass dynamically created type to function correctly?
Dim jsonData = JsonConvert.DeserializeObject(Of List(Of MyCls))(json)
SfDataGrid2.DataSource = jsonData
Catch ex As Exception
End Try
Public Shared Function CreateClass(ByVal className As String, ByVal properties As Dictionary(Of String, Type)) As Type
Dim myDomain As AppDomain = AppDomain.CurrentDomain
Dim myAsmName As New AssemblyName("MyAssembly")
Dim myAssembly As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run)
Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("MyModule")
Dim myType As TypeBuilder = myModule.DefineType(className, TypeAttributes.Public And TypeAttributes.Class)
'Dim getSetAttr As MethodAttributes = MethodAttributes.Public And MethodAttributes.SpecialName And MethodAttributes.HideBySig
Dim getSetAttr As MethodAttributes = MethodAttributes.Public
myType.DefineDefaultConstructor(MethodAttributes.Public)
For Each o In properties
Dim prop As PropertyBuilder = myType.DefineProperty(o.Key, PropertyAttributes.HasDefault, o.Value, Type.EmptyTypes)
Dim field As FieldBuilder = myType.DefineField("_" + o.Key, o.Value, FieldAttributes.Private)
Dim getter As MethodBuilder = myType.DefineMethod("get_" + o.Key, getSetAttr, o.Value, Type.EmptyTypes)
Dim getterIL As ILGenerator = getter.GetILGenerator()
getterIL.Emit(OpCodes.Ldarg_0)
getterIL.Emit(OpCodes.Ldfld, field)
getterIL.Emit(OpCodes.Ret)
prop.SetGetMethod(getter)
Dim setter As MethodBuilder = myType.DefineMethod("set_" + o.Key, getSetAttr, Nothing, New Type() {o.Value})
Dim setterIL As ILGenerator = setter.GetILGenerator()
setterIL.Emit(OpCodes.Ldarg_0)
setterIL.Emit(OpCodes.Ldarg_1)
setterIL.Emit(OpCodes.Stfld, field)
setterIL.Emit(OpCodes.Ret)
prop.SetSetMethod(setter)
Next
Return myType.CreateType()
End Function
Please help with this

VB API Result converts decimal numbers

I have a simple API Post like this
<HttpPost>
Public Function testPost() As IEnumerable(Of sp_select)
Dim _tmplist As New List(Of sp_select)
Dim _Content = jsonSerializer.Deserialize(Of myparams)(Request.Content.ReadAsStringAsync().Result)
Dim mandant = _Content.mandant
Using mycon As New dal_globalDataContext
Dim _result = mycon.sp_select(CType(mandant, Decimal)).ToList
If Not _result.Any = False Then
For Each entry In _result
_tmplist.Add(entry)
Next
End If
End Using
Return _tmplist
End Function
All good. But If I parse the result like this:
Public Sub testPost()
Dim client = New System.Net.Http.HttpClient()
client.BaseAddress = New Uri("http://localhost:62068")
client.DefaultRequestHeaders.Accept.Clear()
client.DefaultRequestHeaders.Accept.Add(New System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"))
Dim _datasource As IEnumerable(Of Object) = Nothing
Dim _content = New With {.mandant = 0}
Dim serializer As New JavaScriptSerializer()
Dim arrayJson As String = serializer.Serialize(_content)
Dim responseTask = client.PostAsync("/actionapi/dataSource/testPost/", New StringContent(arrayJson))
responseTask.Wait()
Dim result = responseTask.Result
If result.IsSuccessStatusCode Then
Dim readTask = result.Content.ReadAsAsync(Of IList(Of sp_select))()
readTask.Wait()
_datasource = readTask.Result
Any decimal property of the class has an decimal point. So 1 will be 1.0
Do you know how to prevent this behaviour?

How build dynamic where clause with string argument

I try to buid a method to add a where clause to a Linq-to-SQL request (return an IQueryable). I try several methods but always use ToString, Indexof... but this result is a sql request take all element and the filter made in linq. I see request in SQL Server profiler.
I want a method to do it with result is a sql request with where include inside
I work in Visual Studio 2017 with SQL Server 2016. I code in vb.net
I see an interesting thing in linq dynamic library. But I can't to adapt to my situation
<Extension()> _
Public Function Where(ByVal source As IQueryable, ByVal predicate As String, ByVal ParamArray values() As Object) As IQueryable
If source Is Nothing Then Throw New ArgumentNullException("source")
If predicate Is Nothing Then Throw New ArgumentNullException("predicate")
Dim lambda As LambdaExpression = DynamicExpression.ParseLambda(source.ElementType, GetType(Boolean), predicate, values)
Return source.Provider.CreateQuery( _
Expression.Call( _
GetType(Queryable), "Where", _
New Type() {source.ElementType}, _
source.Expression, Expression.Quote(lambda)))
End Function
But I don't need all this complex strucutre. It's some years I buid my utilities. But Need to upgrade it. Here my code of my utilities
<Extension()>
Public Function Where(ByVal source As IQueryable, ByVal predicate As String) As IQueryable
Dim param = Expression.Parameter(GetType(String), "x")
Return source.Provider.CreateQuery(
Expression.Call(
GetType(Queryable), "Where",
New Type() {source.ElementType},
source.Expression, Expression.Quote(Expression.Lambda(Expression.Constant(predicate), param))))
End Function
Public Function TFOAppliqueFiltreTri(Of T, MaClassDatas As Class)(Origins As IQueryable(Of T), ByVal MesDonnees As TableFullOption.PagerTabEnCours(Of MaClassDatas)) As IQueryable(of T)
Dim retour As New TableFullOption.LstRetour
'Colonne de filtre
Dim strWh As String = ""
Dim Filtredrecords As IQueryable(Of T)
For Each Sort In MesDonnees.MesOptions
Dim colName = Sort.ColName
If strWh.Length > 0 Then strWh = strWh & " AND "
strWh = strWh & String.Format(colName & " like '%{0}%'", Sort.Search)
Next
If strWh.Length > 0 Then
Filtredrecords = Origins.Where(strWh) '<- Here call Where
Else
Filtredrecords = Origins
End If
Return Filtredrecords
End Function
I get this error:
Aucune méthode générique 'Where' sur le type 'System.Linq.Queryable' n'est compatible avec les arguments de type et les arguments fournis..
Then my problem is to write correctly lambda expression. My predicate argument is : Column1 like '%aaa%'. I want rewrite where method of dynamicLinq to accept string argument :Column1 like '%aaa%' directly
Thanks for your help
Finally after lot of reading in google and few feelings and certainly lot of chance.
Public Function Where(Of TEntity)(source As IQueryable(Of TEntity), searchColumn As List(Of String), searchValue As String) As IQueryable(Of TEntity)
Dim cond As Expression = Nothing
Dim ParamExpr = Expression.Parameter(GetType(TEntity), "x")
Dim conCat2 = GetType(String).GetMethod("Concat", New Type() {GetType(String), GetType(String)})
Dim conCat4 = GetType(String).GetMethod("Concat", New Type() {GetType(String), GetType(String), GetType(String), GetType(String)})
Dim Delim = Expression.Constant("/")
Dim DateName = GetType(SqlFunctions).GetMethod("DateName", New Type() {GetType(String), GetType(Nullable(Of DateTime))})
Dim DatePart = GetType(SqlFunctions).GetMethod("DatePart", New Type() {GetType(String), GetType(Nullable(Of DateTime))})
Dim DblToString = GetType(SqlFunctions).GetMethod("StringConvert", New Type() {GetType(Nullable(Of Double))})
For Each cn In searchColumn
For Each colName In cn.Split("|")
If Not colName.estVide Then
Dim body As Expression = ParamExpr
For Each member In colName.Split(".")
body = Expression.PropertyOrField(body, member)
Next
Dim Tostr As Expression
If body.Type.FullName.Contains("String") Then
Tostr = body
ElseIf body.Type.FullName.Contains("DateTime") Then
Dim day = Expression.Call(Expression.Call(conCat2, Expression.Constant("0"), Expression.Call(DateName, Expression.Constant("day"), body)), "Substring", Nothing, Expression.Constant(0), Expression.Constant(2))
Dim Month = Expression.Call(DatePart, Expression.Constant("MM"), body)
Dim toDouble = Expression.Convert(Month, GetType(Nullable(Of Double)))
Dim mois = Expression.Call(conCat2, Expression.Constant("0"), Expression.Call(Expression.Call(DblToString, toDouble), "Trim", Nothing))
Dim an = Expression.Call(DateName, Expression.Constant("year"), body)
Tostr = Expression.Call(conCat2, Expression.Call(conCat4, day, Delim, mois, Delim), an)
Else
Tostr = Expression.Call(body, "Convert.ToString", Nothing)
'Tostr = Expression.Convert(body, GetType(String))
End If
Dim condPart = Expression.Call(Expression.Call(Tostr, "ToLower", Nothing), "Contains", Nothing, Expression.Call(Expression.Constant(searchValue), "ToLower", Nothing))
If cond Is Nothing Then
cond = condPart
Else
cond = Expression.OrElse(cond, condPart)
End If
End If
Next
Next
Return source.Provider.CreateQuery(Of TEntity)(Expression.Call(GetType(Queryable), "Where", New Type() {GetType(TEntity)}, source.Expression, Expression.Lambda(cond, ParamExpr)))
End Function
Now I've dynamic filter which generated a SQL request with complete clause where

Converting String to Structure

I am successfully converted structure data to string here in plain and (as suggested) in XML serialized way which obviously every has it's own good and bad side effects.
This is sample structure:
Public Structure myList
Dim a As String
Dim b As Integer
Dim c As Double
End Structure
Dim myInstance As New myList
myInstance.a = "Nemo"
myInstance.b = 10
myInstance.c = 3.14
Now I have 2 functions for converting structure data to string:
Dim xString As String = oStructToString(myInstance)
Public Function oStructToString(ByVal obj As Object) As String
Dim structString As String = ""
Dim i As Integer
Dim myType As Type = obj.GetType()
Dim myField As System.Reflection.FieldInfo() = myType.GetFields()
For i = 0 To myField.Length - 1
structString &= myField(i).GetValue(obj)
If i = myField.Length - 1 Then Exit For
structString &= Convert.ToChar(161)
Next i
Return structString
End Function
Dim xString As String = xStructToString(myInstance)
Public Function xStructToString(ByVal obj As Object) As String
Dim x As New Xml.Serialization.XmlSerializer(obj.GetType)
Dim sw As New IO.StringWriter()
x.Serialize(sw, obj)
Return sw.ToString
End Function
But I can't get data back from string to structure.
Public Function oStringToStruct(ByVal xString As String) As Object
So I can call:
Dim mySecondInstance As New myList = oStringToStruct(xString)
Or
Dim mySecondInstance As New myList = xStringToStruct(xString)
How to do that?
So far I came to this:
Public Function xStringToStruct(ByVal xString As String) As Object
Dim x As New Xml.Serialization.XmlSerializer() ''<- what here?
Dim sr As New IO.StringReader(xString)
Return x.Deserialize(sr)
End Function
and this...
By help of har07 still one error remains here...
Public Function oStringToStruct(ByVal xString As String, ByVal type As Type) As Object
Dim structString() As String = xString.Split(Convert.ToChar(161))
Dim myType As Type = type.GetType()
Dim myField As System.Reflection.FieldInfo() = myType.GetFields()
For i As Integer = 0 To structString.Length - 1
myField(i).SetValue(type, structString(i)) ''here crashes
Next i
Return type
End Function
Deserialize xml string back to struct is easier :
Public Function xStringToStruct(ByVal xString As String, ByVal type As Type) As Object
Dim x As New Xml.Serialization.XmlSerializer(type)
Dim sw As New IO.StringReader(xString)
Return x.Deserialize(sw)
End Function
You can use it like so :
Dim xObject As myList = xStringToStruct(xString, GetType(myList))

How can pass collection list between aspx pages?

I tried to create list of object type and get through session the class type list from another page,but it is not working simply coming out of the code when assigment is done.
Public Sub GetvalueContains(ByVal txtFilterText As String, ByVal strColPropertyName As String, ByVal collectionName As String)
Dim FilteredAgentsList As New List(Of Object)
FilteredAgentsList = CType(HttpContext.Current.Session("FilteredAgentsList"), Object)
AgentList = Session("AgentList")
FilteredAgentsList = CType(HttpContext.Current.Session("AgentList"), Object)
Dim FilteredAgentsListdetails As New List(Of Object)
FilteredAgentsListdetails = HttpContext.Current.Session("FilteredAgentsListdetails")
Dim indx As Integer = 0
Dim indexx As Integer = collectionName.IndexOf("_")
indexx = indexx + 1
Dim str As String = collectionName.Substring(indexx, collectionName.Length - indexx)
Dim grdagents As GridView = HttpContext.Current.Session("grdAgents")
If FilteredAgentsListdetails.Count > 0 Then
FilteredAgentsList = FilteredAgentsListdetails
End If
Dim AgentListTemp As List(Of Object) = HttpContext.Current.Session("AgentListTemp")
AgentListTemp = FilteredAgentsList
'FilteredAgentsListdetails = New List(Of Agents)
strColPropertyName = GetPropertyNameAtIndex(AgentListTemp, Convert.ToInt32(hdncellindex.Value))
txtFilterText = txtFilterText.Replace("*", "")
Dim resultList As IEnumerable(Of Object)
resultList = AgentListTemp.Where(Function(x) x.[GetType]().GetProperty(strColPropertyName).GetValue(x, Nothing).ToString().StartsWith(txtFilterText))
'AgentListTemp = CType(resultList.ToList(), List(Of Agents))
'FilteredAgentsListdetails = AgentListTemp
grdagents.DataSource = Nothing
grdagents.DataBind()
grdagents.DataSource = resultList
grdagents.DataBind()
End Sub
how to get the session value in above code?or any other method to pass list from one page to other.