none
Dynamic Columns in aggregate Function RRS feed

  • Question

  • Hi,
    I want the sum of three columns. The column name has to be constructed at the Run-time. The grid has data as follows

    Description               Material Amt                Labour Amt              SubContractor Amt                Total
     ABC                             10                                  22                             30
     SYC                             40                                  44                             85

    I want the Sum of Material AMt, Labour Amt , SubCOnstract AMt row to be displayed in the Total Column.
    The Name of the Columns Material , Labour etc. are to be attached dynamically. I have a master table say 'Type' which contains the name of these columns.

    At the run time I have  construct the Name of the COlumns and do the sum. I tried the Following code 

     

    Dim Markup = dsMarkup.Tables(0).AsEnumerable()

     

    Dim categories = From p In Markup _

     

    Group p By Key = p.Field(Of Int32)("BidDetailID") Into Group _

     

    Select BidDetailID = Key, _

    Total =

    Group.Sum(Function(p) p(5) + p(6))


    Here I have hard-code the Column Index to 5 and 6, but at the run time i wont know the column index . How do I take the columns index / name at run time ?

          
     
    Thursday, July 16, 2009 10:21 AM

Answers

All replies

  • Take a look at System.Linq.Dynamic - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    Friday, July 17, 2009 7:49 AM
    Answerer
  • Hi Kristofer,

    Thank you for your help, the link is very helpful.

    In the sample application I got an Project for Dyamic query, I tried consuming the Dynamic.vb class in another project, but it gives me error. I am Developing an windorws application (VB.net), the moment I add this class, on the form load event or any other events it gives me an error for. e.g

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

    End Sub

    For System.Object or System.EventArgs, i am getting error and it suggests me to change the System.Object to Global.System.Object. I don't know where i am going wrong. Do I have to add any reference ? Please note I would be using LINQ to Dataset. Also another query is the Northwind Class is being added, do I have to add such class ?

    Monday, July 20, 2009 11:41 AM