如何制作字段类型变体 [英] how to make field type variant

查看:55
本文介绍了如何制作字段类型变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊重Sir / s,



我在我的Windows应用程序中使用linq使用vb.net

我在其中制作了一个所有表格的通用通用程序,

我要传递FinalDT数据表。

这里每一次FinalDT中一列OSQnty的数据类型从Integer变为Double或者Int32..since数据将从不同的表中获取..

但我不能每次为不同的表写新的查询。



.OSQnty = abc.Sum(函数(f)f.Field(Of Integer)(OSQnty))}



我怎么能摆脱这个...?





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..

推荐答案

这可能不是很漂亮,但是这里有一种方法可以获得我认为你的打字结果想。



而不是在查询中直接调用abc.Sum(..),而是将abc传递给函数(本例中为TallyObjects)以选择正确键入的函数求和方法。我创建了一个简单的测试DataTable来演示该技术,它与您的表模式不匹配,但希望您能理解所使用的技术。随意提问。



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


这篇关于如何制作字段类型变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆