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 7737849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:06:41+00:00 2026-06-01T08:06:41+00:00

I have a linq to sql query that produces duplicates. i can seem to

  • 0

I have a linq to sql query that produces duplicates. i can seem to figure out how to eliminate these duplicate records. Maybe someone on here can help. The idea is to retrieve the sum of the “total” from table2 that occurs for dates in table 1 that are greater than dates of table2 and also have dates in table2 that are greater than 2011-01-01. What i have achieves this but because of duplicates i get exactly double the sum. Your help is much appreciated

Table structure:

table1:  id, date
table2: id, date, total

The query

from t1 in table1
                    join t2 in table2 on t1.id equals t2.id into x
                    from y in x
                    where t1.id == "123" && t1.date > y.date && y.date > DateTime.Parse("2011-01-01") 
                    group t1 by new { y.total} into g

                    select g.Sum(t1 => t1.total);
  • 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-01T08:06:42+00:00Added an answer on June 1, 2026 at 8:06 am

    You should split the query in multiple steps:

    Select the data you want:

    var data = 
    from t1 in table1
    join t2 in table2 on t1.Id equals t2.Id
    where t1.Id == "123" && t1.Date > t2.Date && t2.Date > DateTime.Parse("2011-01-01")
    select new {t1.Id, t2.Date, t2.Total};
    

    Call distinct to avoid repeating items:

    var distinct = data.Distinct();
    

    Get the sum:

    return distinct.GroupBy(x => x.Date)
                    .Select(g => g.Sum(item => item.Total));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LINQ to SQL query which produces an object of type {System.Data.Linq.DataQuery}.
I have a linq-to-sql query that groups results by DateTime, something like this: var
I have a LINQ to SQL query that when executed does not return any
I have a very complex Linq to SQL query that returns a result set
I have a linq-to-sql query that I use to fill an object that has
I have a LINQ to SQL query that returns a grouped collection of Sponsor
I have a Premiums table that I attempting to query using the following LINQ-to-SQL:
How do I filter my Linq-to-SQL query so that it only shows records that
I have a LINQ from SQL query that returns sorted rows. For instance, it
I have the following LINQ conditional where clause query that produces a result of

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.