如何使用实体框架将列的SUM转换为SELECT查询? [英] How to get SUM of a column into SELECT query using entity framework ?

查看:93
本文介绍了如何使用实体框架将列的SUM转换为SELECT查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Select Query中获得股票总和。



i创造了这个类:

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,

           });







但出现此错误:



'view_expireditems_noserials'不包含'Sum'的定义,并且没有可访问的扩展方法'Sum'接受类型为'view_expireditems_noserials'的第一个参数'(您是否缺少using指令或汇编参考?)

推荐答案

你用Sum()扩展x; x不是集合。



您将Sum()应用于集合。



In你的情况,它将与你的.Where处于同一水平......它不会在详细程度计算。
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".


这篇关于如何使用实体框架将列的SUM转换为SELECT查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆