Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Respected Sir/s,

I am using linq in my windows application using vb.net
in which i have made one common generic procedure for all tables,
in which i am going to pass FinalDT datatable.
here everytime datatype of one column "OSQnty" in FinalDT is change from Integer to Double or Int32..since data will be fetched from different different tables..
but i cant write new query everytime for different tables.

.OSQnty = abc.Sum(Function(f) f.Field(Of Integer)("OSQnty"))}

how can i rid of this..?


public sub GetQueryResult(FinalDT as Datatable)

 Dim fq = From FU In FinalDT.AsEnumerable() _
           Order By FU("IName") _
           Group By grpIName = FU("IName"), grpSize = FU("Size") Into abc = Group _
           Select New With {.IName = grpIName, .Size = grpSize, .Unit = grpUnit, .MRP = grpMRP,    .OSQnty = abc.Sum(Function(f) f.Field(Of Integer)("OSQnty"))}

  DGV.DataSource = fq.ToList()


Please help me...
Thank you..
Posted
Comments
Sergey Alexandrovich Kryukov 11-Dec-13 15:24pm    
Variant type is evil. Why would you even need it, in .NET?
—SA

1 solution

Well this may not be pretty, but here is one way to get the typed result that I think you want.

Instead of directly calling abc.Sum(..) in the query, "abc" is passed to a function (TallyObjects in this example) to select the properly typed Sum method. I created a simple test DataTable to demonstrate the technique, It does not match your table schema, but hopefully you will be understand the technique used. Feel free to ask questions.

Private Sub Test()
   ' create a DataTable with data to test
   Dim dt As New DataTable()
   With dt
      .Columns.Add("c1", GetType(String))
      .Columns.Add("OSQnty", GetType(Double))
      .Rows.Add(New Object() {"a4", 23})
      .Rows.Add(New Object() {"a2", 7})
      .Rows.Add(New Object() {"a2", 7})
      .Rows.Add(New Object() {"a1", 30})
      .AcceptChanges()
   End With
   GetQueryResult(dt) ' simulate calling OP's method
End Sub 'Test

Private Sub GetQueryResult(ByVal dt As DataTable)
   Dim results As IEnumerable = _
      (From r In dt.AsEnumerable() _
      Order By r("c1") _
      Group By col = r("c1") Into abc = Group _
      Select New With {.Name = col, .Tally = TallyObjects(abc, "OSQnty")}).ToList

   DataGridView1.DataSource = results
End Sub 'GetQueryResult

Private Function TallyObjects(ByVal rows As IEnumerable(Of DataRow), ByVal columnname As String) As Object
   Dim ret As Object = Nothing
   If rows.Count > 0 Then
      ' select the sumation function based on the DataType of OSQnty
      Select Case Type.GetTypeCode(rows(0)("OSQnty").GetType)
         Case TypeCode.Double
            ret = rows.Sum(Function(dr As DataRow) CDbl(dr.Item(columnname)))
         Case TypeCode.Int32
            ret = rows.Sum(Function(dr As DataRow) CInt(dr.Item(columnname)))
         ' add a "Case" for any other sumable types you may need
      End Select
   End If
   Return ret
End Function 'TallyObjects
 
Share this answer
 
Comments
Sunil Bansode 12-Dec-13 3:27am    
Thank you very very much...Its works..
i was pulling my hair of head since 4-5 days for this..
Once again thanks and God Bless You....!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900