Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8692987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:26:23+00:00 2026-06-13T00:26:23+00:00

Hi i need dynamic linq queries which has group by in vb.net. My queries

  • 0

Hi i need dynamic linq queries which has group by in vb.net.

My queries are as following.

In first query i need to group by two columns, but in second i need to group by just one column. It depens on how user wants to see the report.

How can i produce these queries on runtime according to the user choices?

Thanks in advance.

Dim query = From r In dtString.AsEnumerable
        Group r By Key0 = r.Field(Of String)("country"),
                   Key1 = r.Field(Of String)("region") Into Group
        Select Key0, Key1,
               DebitTotal = Group.Sum(Function(r) r.Field(Of Decimal)("debit")),
               CreditTotal = Group.Sum(Function(r) r.Field(Of Decimal)("credit"))


Dim query = From r In dtString.AsEnumerable
        Group r By Key0 = r.Field(Of String)("country") Into Group
        Select Key0
               DebitTotal = Group.Sum(Function(r) r.Field(Of Decimal)("debit")),
               CreditTotal = Group.Sum(Function(r) r.Field(Of Decimal)("credit"))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:26:24+00:00Added an answer on June 13, 2026 at 12:26 am

    As you can see from your Select clauses, the result of your two queries are not (anonymous) type compatible. Your simplest solution may be to alter your second query to something that does have a second redundant or constant group key.

    E.g.

    query = From r In dtString.AsEnumerable
        Group r By Key0 = r.Field(Of String)("country"),
                   Key1 = r.Field(Of String)("country") Into Group
        Select Key0, Key1,
               DebitTotal = Group.Sum(Function(r) r.Field(Of Decimal)("debit")),
               CreditTotal = Group.Sum(Function(r) r.Field(Of Decimal)("credit"))
    

    or

    query = From r In dtString.AsEnumerable
        Group r By Key0 = r.Field(Of String)("country"),
                   Key1 = "ignored" Into Group
        Select Key0, Key1,
               DebitTotal = Group.Sum(Function(r) r.Field(Of Decimal)("debit")),
               CreditTotal = Group.Sum(Function(r) r.Field(Of Decimal)("credit"))
    

    (untested)

    Then you’ll have to adjust your result display to ignore key1 when required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to translate the following LINQ query to Dynamic LINQ that accepts several
I need to build a dynamic linq query with or operators. I have seen
I have not used LINQ before and have need to do dynamic queries on
I need to used dynamic order query in mysql and i have successfully achieved
I need to add dynamic controls and get its values in my asp.net mvc
I need to create a dynamic array in Java, but the values type differ
We have a project using LINQ to SQL, for which I need to rewrite
I need to dynamically set the sproc name of a Linq to SQL query
Say i need to filter a generic list with a dynamic query ( List<string>
I'm trying to use PredicateBuilder to compose dynamic linq queries. In my object, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.