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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:30:51+00:00 2026-05-30T18:30:51+00:00

I have the following query working fine in LINQ to SQL. Now I want

  • 0

I have the following query working fine in LINQ to SQL. Now I want to change it to Entity Framework

var _sale = from emp in setupEmployees
            join sales in vwSaleTargets on emp.EmployeeID equals sales.EmployeeID
            join price in vwPeriodPricings
               on new { sales.SKUID, sales.PeriodID } 
               equals new { SKUID = (int?)price.SKUID, PeriodID = (int?)price.PeriodID }
            join sk in setupSKUs on sales.SKUID equals sk.SKUID
            join br in setupBrands on sk.BrandID equals br.BrandID
            where emp.EmployeeID == 123 && sales.StartDate.Year == 2012 
            select new { emp, sales, price, sk, br };

var lstSale = _sale.ToList(); //to avoid n+1 queries in case of grouping
var sale2 = from x in lstSale
            group x by new { x.sk, x.emp } into grouping
            select new 
            {
                 EmployeeName = grouping.Key.emp.EmployeeName,
                 SKUID = grouping.Key.sk.SKUID,
                 SKUName = grouping.Key.sk.Title,
                 MonthSale =(double?)grouping
                          .Where(x => x.sales.StartDate.Month == 2 && 
                                      x.sales.StartDate.Year == 2012)
                          .Select(t=>t.sales.SalesQuantity)
                          .Sum(t=>t.Value)?? 0,
                 MonthSaleValue = (double?)grouping
                          .Where(x => x.sales.StartDate.Month == 2 && 
                                      x.sales.StartDate.Year == 2012)
                          .Sum(x => x.sales.SalesQuantity * x.price.ExFactoryPrice)  
                             ?? 0,
            };
Console.WriteLine(sale2.OrderBy(x => x.SKUName).ToList());

In Entity Framework It is giving me result Like this

Name SKUID SKUName MonthSale MonthSaleValue 
EMP1  36    SKU1     113     61375.95 
EMP1  17    SKU2     113     6656.83 
EMP1  18    SKU3     113     9984.68 
EMP1  19    SKU4     113     15169.12 

In L2S I am getting me correct result like this

Name SKUID SKUName MonthSale MonthSaleValue 
    EMP1  36    SKU1     74     40193.1 
    EMP1  17    SKU2     113     6656.83 
    EMP1  18    SKU3     461     40733.96
    EMP1  19    SKU4     2     268.48

Regards

  • 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-05-30T18:30:53+00:00Added an answer on May 30, 2026 at 6:30 pm

    As an approach to finding the answer…

    To diagnose, as suggested by @Jon Skeet, you need to simplify it AND look at what you’re getting within lstSale to compare LINQ to SQL to EntityFramework.

    So something along the following lines may help (not necessarily syntactically correct because I haven’t got all your source objects to check, however I’m just looking at the query and simplifying it down where it appears you can)

    var _sale = from emp in setupEmployees
                join sales in vwSaleTargets on emp.EmployeeID equals sales.EmployeeID
                join price in vwPeriodPricings
                   on new { sales.SKUID, sales.PeriodID } 
                   equals new { SKUID = (int?)price.SKUID, PeriodID = (int?)price.PeriodID }
                join sk in setupSKUs on sales.SKUID equals sk.SKUID
                where emp.EmployeeID == 123 && sales.StartDate.Year == 2012 && sales.StartDate.Month == 2
                select new 
                { 
                    EmployeeName = emp.EmployeeName, 
                    StartDate = sales.StartDate,
                    SalesQuantity = sales.SalesQuantity, 
                    ExFactoryPrice = price.ExFactoryPrice, 
                    SKUID = sk.SKUID,
                    SKUName = sk.SKUName 
                };
    
    var lstSale = _sale.ToList(); //to avoid n+1 queries in case of grouping
    
    // Run through lstSale here
    foreach(var item in lstSale)
    {
      Console.WriteLine(item);
    }
    
    var sale2 = from x in lstSale
                group x by new { x.SKUID, x.EmployeeName } into grouping
                select new 
                {
                     EmployeeName = grouping.Key.EmployeeName,
                     SKUID = grouping.Key.SKUID,
                     SKUName = grouping.SKUName,
                     MonthSale =(double?)grouping
                              .Where(x => x.StartDate.Month == 2 && 
                                          x.StartDate.Year == 2012)
                              .Select(t=>t.SalesQuantity)
                              .Sum(t=>t.Value)?? 0,
                     MonthSaleValue = (double?)grouping
                              .Where(x => x.StartDate.Month == 2 && 
                                          x.StartDate.Year == 2012)
                              .Sum(x => x.SalesQuantity * x.ExFactoryPrice)  
                                 ?? 0,
                };
    Console.WriteLine(sale2.OrderBy(x => x.SKUName).ToList());
    

    Changes (which may not all be valid):
    1. Removed branding since it isn’t consumed in the second query (you could use it as a join in the first but not add to the new type if its part of the restriction)
    2. Simplified what’s included in the anonymous type generated by the first query – if you’re only consuming parts of emp/sales/price then it may make it clearer as to what’s going on
    3. Added restriction to SalesMonth in the first part (what you’re doing in the second) because that may reduce your data, increase performance and allow you to focus on what’s actually going wrong (I have left the second SalesMonth restriction in place)
    4. I assume SKUID is the relevant part of sk for grouping and not all of the object is required

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

Sidebar

Related Questions

I have the following query which is working fine, but I also want to
I have a SQl query working fine, I need to convert it to LINQ
I have the following query and it is working fine. If you see the
I have following query , select * from process where name like 'abc'; now
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following dynamic query which is working fine without the WHERE clause,
I want to convert Following sql server query with Linq-to-sql query . What I
I have the following xpath query which seems to be working but I just
I have following query: $query=SELECT language_value, votes, user_id FROM labels WHERE approved=1 AND label_value=.
I have the following query to count all data every minute. $sql= SELECT COUNT(*)

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.