按从一个表中选择金额

rnmwe5a2  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(323)

我想选择金额 CustomerID , Category , Store 以及 City .
如果可以,请更正此查询?

var query = db.Amounts.Where(x => x.CustomerID == Convert.ToInt32(lkpCustomer.EditValue) &&
                  x.CatagoryID == Convert.ToInt32(lkp_Catagory.EditValue) && x.StoresID == Convert.ToInt32(lkp_from.EditValue) &&
                  x.CityID == Convert.ToInt32(lkp_to.EditValue)).Select(x => x.Amount1).ToList();
RateSpinEdit.EditValue = query;

说明:

goqiplq2

goqiplq21#

如果我没弄错的话,你需要把 amount 按以下字段列出: CustomerID , Category , Store ,和 City .
在这种情况下,以下代码可能是您问题的答案:

var query = (from p in Amounts
         group p by new { p.CustomerID, p.CatagoryID, p.StoresID, p.CityID}
         into grp
         where 
         (
            p => p.CustomerID    == Convert.ToInt32(lkpCustomer.EditValue) &&
                 p.CatagoryID    == Convert.ToInt32(lkp_Catagory.EditValue) &&
                 p.StoresID      == Convert.ToInt32(lkp_from.EditValue) &&
                 p.CityID        == Convert.ToInt32(lkp_to.EditValue)            
         )
         select new 
         {
                grp.key.ID,
                grp.key.CategoryID,
                grp.key.CustomerID,
                grp.key.StoresID,
                grp.key.CityID,
                SumOfAmount = grp.Sum(p => p.Amounts)            
         }).ToList();
vqlkdk9b

vqlkdk9b2#

谢谢大家的帮助。我使用这段代码,它成功地工作了:
使用(var db=new dbdatacontext()){var datasource=db.amounts;

var query = from amount in datasource
                        where amount.CatagoryID == Convert.ToInt32(lkp_Catagory.EditValue)&&
                        amount.CustomerID == Convert.ToInt32(lkpCustomer.EditValue)&&
                        amount.StoresID == Convert.ToInt32(lkp_from.EditValue)&&
                        amount.CityID == Convert.ToInt32(lkp_to.EditValue)
                        select amount;
            RateSpinEdit.EditValue = query.Select(x => x.Amount1).FirstOrDefault();

相关问题