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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:38:51+00:00 2026-06-02T22:38:51+00:00

I have a query that should return a sum of total hours reported for

  • 0

I have a query that should return a sum of total hours reported for the current week.
This code below returns the Correct total of hours but not for a specific user in the database.

    public int reportedWeekTime(int currentWeek, string username)
        {
            var totalTime = (from u in context.Users
                         from r in context.Reports
                         from w in context.Weeks
                         from d in context.Days
                         where u.Id == r.UserId && r.weekNr.Equals(currentWeek) && r.Id   == w.ReportId && w.DayId == d.Id
                         select d.Hour).DefaultIfEmpty(0).Sum();
            return totalTime;
        }

The first method returns the number 24, wich is correct but as i said, not for a specific user.

I am trying to do this, but it gives me 0 in return. What am i doing wrong?

    public int reportedWeekTime(int currentWeek, string username)
        {
            var totalTime = (from u in context.Users
                         from r in context.Reports
                         from w in context.Weeks
                         from d in context.Days
                         where u.Id == r.UserId && r.weekNr.Equals(currentWeek) && r.Id == w.ReportId && w.DayId == d.Id && u.Username.Contains(username)
                         select d.Hour).DefaultIfEmpty(0).Sum();
            return totalTime;
        }
  • 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-02T22:38:52+00:00Added an answer on June 2, 2026 at 10:38 pm

    Update – Troubleshooting approach, create a new anonymous class with the u.Username property, the string username, and the comparison. It will be easier to visualize what is going on

    var users = (from u in context.Users
                 select new
                 { 
                   UsernameDb = u.Username,
                   UsernameSearch = username,
                   Comparison = u.Username.Contains(username),
                 }).ToList();
    

    Original

    I would modify your query slightly:

    1. Use join‘s instead of from‘s with where clauses
    2. Remove the DefaultIfEmpty(0)

    (1) Is more for readability, but I think (2) is the cause of your problem

    var totalTime = (from u in context.Users
                     join r in context.Reports on u.Id equals r.UserId
                     join w in context.Weeks on r.Id equals w.ReportId
                     join d in context.Days on w.DayId equals d.Id
                     where r.weekNr.Equals(currentWeek) && u.Username.Contains(username)
                     select d.Hour).Sum();
    

    I would also make sure that the following query returns result. If not, than that would be your problem

    var users = from u in context.Users
                where u.Username.Contains(username)
                select u;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that I know it should atleast return one row. How
I have a query that returns a result set similar to the one below:
i've a MySQL query that should return some rows that have the letters Ö
if you have a select LINQ query that should only return one result, do
i have a query that should return 40 results: $test = mysql_query( SELECT fname
I have a query that based on various ids within a table should return
Ok I have a query that should return all the issue numbers. What I
I have a query that looks a bit like this: SELECT weekEnd, MAX(timeMonday) FROM
I have a query that uses a where clause. At times, this may be
Hi I have written this query SELECT cl_brands.name AS Brand,DATE_FORMAT(cl_doctor_call.date_entered,'%b%y')AS MonthYear, SUM(FIND_IN_SET(CONCAT('^',cl_brands.id,'^'),cl_doctor_call_cstm.brand_discussed_c))AS No_Times_Brand_Discussed FROM

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.