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

  • Home
  • SEARCH
  • 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 6978493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:45:52+00:00 2026-05-27T17:45:52+00:00

I have a entity called StockDetails using Entity Framework, see picture below I want

  • 0

I have a entity called StockDetails using Entity Framework, see picture below

I want to fetch a list IEnumerable<StockDetail>, summarized by Reels, Qtyton, average date from Days (datetime) and grouping by the rest of the properties.

I’m building a datalayer (WCF Services) with Entity Framework as ORM, some of the services are old SQL queries I’m trying to convert to linq/lamdba expression. But I’m pretty new to how to write and want some help.

This is how I started the query in lambda, but I got stuck on the groupby/sum/average part.

public IEnumerable<StockDetail> ListStockDetailByCustomerNumber(int customerNumber)
{
   var CustNo = customerNumber.ToString();

   return _entities.StockDetails
                .Where(x => x.Custno == CustNo)
                .GroupBy(
                    x =>
                    new
                        {
                            x.Millcd,
                            x.Matercd,
                            x.Proddesc,
                            x.Grammage,
                            x.Reelwidth,
                            x.Ordercode,
                            x.Buyordno,
                            x.Whsedesc,
                            x.Co,
                            x.Finished,
                            x.Pm,
                            x.PurchaseOrder,
                            x.Diameter,
                            x.Rtadate,
                            x.Custno,
                            x.Reels,
                            x.Days,
                            x.Qtyton
                        })
                .ToList();

        }

Question solved:

public IEnumerable<StockDetail> ListStockDetailByCustomerNumber(int customerNumber)
        {
            var stockDetailsList = new List<StockDetail>();
            var custNo = customerNumber.ToString();

            var list = _entities.StockDetails
                       .Where(x => x.Custno == custNo )
                       .GroupBy(
                           x =>
                           new
                               {
                                   x.Millcd,
                                   x.Matercd,
                                   x.Proddesc,
                                   x.Grammage,
                                   x.Reelwidth,
                                   x.Ordercode,
                                   x.Buyordno,
                                   x.Whsedesc,
                                   x.Co,
                                   x.Finished,
                                   x.Pm,
                                   x.PurchaseOrder,
                                   x.Diameter,
                                   x.Rtadate,
                                   x.Custno,
                                   x.UpdDte
                               })
                       .Select(x => new
                                        {
                                            x.Key.Millcd,
                                            x.Key.Matercd,
                                            x.Key.Proddesc,
                                            x.Key.Grammage,
                                            x.Key.Reelwidth,
                                            x.Key.Ordercode,
                                            x.Key.Buyordno,
                                            Reels = x.Sum(p => p.Reels),
                                            Qtyton = x.Sum(p => p.Qtyton),
                                            Day = x.Max(p => p.Days),
                                            //Day = x.Average(p => p.Days.Ticks), // Want to calculate average datetime of date but linq dosn't support datetime.ticks
                                            x.Key.Whsedesc,
                                            x.Key.Co,
                                            x.Key.Finished,
                                            x.Key.Pm,
                                            x.Key.PurchaseOrder,
                                            x.Key.Diameter,
                                            x.Key.Rtadate,
                                            x.Key.Custno,
                                            x.Key.UpdDte
                                        });


            foreach (var s in list)
            {
                stockDetailsList.Add(new StockDetail
                                         {
                                             Millcd  = GetFriendlyNameForKey(s.Millcd),
                                             Matercd = s.Matercd,
                                             Proddesc = s.Proddesc,
                                             Grammage = s.Grammage,
                                             Reelwidth = s.Reelwidth,
                                             Ordercode = s.Ordercode,
                                             Buyordno = s.Buyordno,
                                             Reels = s.Reels,
                                             Qtyton = s.Qtyton,
                                             Days = s.Day,
                                             Whsedesc = s.Whsedesc,
                                             Co = s.Co,
                                             Finished = s.Finished,
                                             Pm = s.Pm,
                                             PurchaseOrder = s.PurchaseOrder,
                                             Diameter = s.Diameter,
                                             Rtadate = s.Rtadate,
                                             Custno = s.Custno,
                                             UpdDte = s.UpdDte
                                         });
            }

            return stockDetailsList;
        }

enter image description here

This is how the query looks in T-SQL

SELECT 
   Millcd, Matercd,
   Proddesc, Grammage,
   Reelwidth, Ordercode,
   Buyordno,
   SUM(Reels) as Reels,
   SUM(Qtyton) as Qtyton,
   Whsedesc, Co, 
   (cast(FLOOR(avg(cast(DateProd as float))) as datetime)) As Days,
   Finished, Pm,
   PurchaseOrder,
   Diameter, Rtadate,
   Custno, UpdDte
FROM StockDetail
WHERE custno = @custcode
GROUP BY Millcd, Matercd, Proddesc, Grammage, Reelwidth, Ordercode, Buyordno, 
         Whsedesc, Co, Finished, Pm, PurchaseOrder, Diameter, Rtadate, Custno, UpdDte
  • 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-27T17:45:52+00:00Added an answer on May 27, 2026 at 5:45 pm

    not sure if this will help you but you can add

      Reels = (_entities.StockDetails 
                .Where(x => x.Custno == CustNo).Sum(x=>x.Reels)) 
    

    instead of x.Reels in your select , and do the same with Qtyton

    For your average use the average extension

    your select will look something like .Select(x=>new {...}) after your where statement then the group by

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

Sidebar

Related Questions

I have an entity called Incident, as you can see on the picture, it
I have Entity Framework entities Events which have an EntityCollection of RSVP. I want
Using Doctrine 2. I have an entity called 'size' and I'm trying to form
I have an entity called User which can have a list of other Users
Right now I'm using Doctrine2 in conjunction with Symfony2. I have an entity called
I have an entity called Category and the entity contains a IEnumerable called ChildCategories.
I'm using .NET MVC 3 Code-First approach. My domain have a entity called Question
I'm using Hibernate/JPA and have an @Entity object called Order, pointing at a MySQL
I have an abstract entity called Block which contains two attributes: column and order
I have a db.BlobProperty property (called Icon) in my entity which contains an image

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.