Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi, i want to get the sum of stocks in Select Query.

i created this class :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MrSales.MrSModels.dataObjects
{
    class CLSExpiredStocks
    {           
							
        private int _stitems_ID;
        private int _stitems_Status;
        private string _stitems_Name;
        private int _stitems_Type;
        private string _stitems_Type_name;
        private string _stitems_Code;
        private string _stitems_NationalCode;
        private int _DetunitID;
        private string _UnitName;
        private int _StoreID;
        private string _store_Name;
        private DateTime _ExpireDate;
        private int _Stock;
        private double _Remain;

        public int stitems_ID
        {
            get { return this._stitems_ID; }
            set { this._stitems_ID = value; }
        }
        public int stitems_Status
        {
            get { return this._stitems_Status; }
            set { this._stitems_Status = value; }
        }
        public string stitems_Name
        {
            get { return this._stitems_Name; }
            set { this._stitems_Name = value; }
        }
        public int stitems_Type
        {
            get { return this._stitems_Type; }
            set { this._stitems_Type = value; }
        }
        public string stitems_Type_name
        {
            get { return this._stitems_Type_name; }
            set { this._stitems_Type_name = value; }
        }
        public string stitems_Code
        {
            get { return this._stitems_Code; }
            set { this._stitems_Code = value; }
        }
        public string stitems_NationalCode
        {
            get { return this._stitems_NationalCode; }
            set { this._stitems_NationalCode = value; }
        }
        public int DetunitID
        {
            get { return this._DetunitID; }
            set { this._DetunitID = value; }
        }
        public string UnitName
        {
            get { return this._UnitName; }
            set { this._UnitName = value; }
        }
        public int StoreID
        {
            get { return this._StoreID; }
            set { this._StoreID = value; }
        }
        public string store_Name
        {
            get { return this._store_Name; }
            set { this._store_Name = value; }
        }
        public DateTime ExpireDate
        {
            get { return this._ExpireDate; }
            set { this._ExpireDate = value; }
        }
        public int Stock
        {
            get { return this._Stock; }
            set { this._Stock = value; }
        }
        public double Remain
        {
            get { return this._Remain; }
            set { this._Remain = value; }
        }
    }
}


What I have tried:

DateTime date = FromInput.DateTime;
           mrsalesdbEntities DB1 = ConnectionTools.OpenConn();
           var Query = DB1.view_expireditems_noserials.Where(u => u.ExpireDate <= date).Select(x => new CLSExpiredStocks
           {
               stitems_ID = x.stitems_ID,
               stitems_Status = x.stitems_Status.Value,
               stitems_Name = x.stitems_Name,
               stitems_Type = x.stitems_Type.Value,
               stitems_Type_name = strings.STOCKS,
               stitems_Code = x.stitems_Code,
               stitems_NationalCode = x.stitems_NationalCode,
               DetunitID = x.DetunitID.Value,
               UnitName = x.UnitName,
               StoreID = x.StoreID.Value,
               store_Name = x.store_Name,
               ExpireDate = x.ExpireDate.Value,
               Stock = x.Sum(i => i.Stock),
               Remain = date.Subtract(x.ExpireDate.Value).TotalDays,

           });



but this error appear :

'view_expireditems_noserials' does not contain a definition for 'Sum' and no accessible extension method 'Sum' accepting a first argument of type 'view_expireditems_noserials' could be found (are you missing a using directive or an assembly reference?)
Posted
Updated 24-May-19 9:27am

1 solution

You "extended" x with Sum(); x is not a "collection".

You apply Sum() to a collection.

In your case, it would be at the same level as your ".Where" ... it does not compute at the "detail level".
 
Share this answer
 
Comments
Golden Basim 24-May-19 15:32pm    
how to do that and show the sum() within the columns ? please can you give me example
[no name] 24-May-19 15:36pm    
"show ... within the columns". I have no clue to what that might mean.

Take some time to formulate a proper question.

If you can't do something in ONE step, take TWO or THREE.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900