我有这张桌子:
Con Fecha Rid Pdv Pla Descripcion Total Quantity
----------------------------------------------------------------------------
1 2016-01-01 COMIDAS FUEM 184 POZ ROJO 85 1
10 2016-01-01 DESAYUNOS VTSI 184 POZ ROJO 85 4
100 2016-01-01 COMIDAS VTSI 693 ENVASE 1 LT 10 1
101 2016-01-01 DESAYUNOS REST 693 ENVASE 1 LT 10 2
102 2016-01-01 COMIDAS VTSI 693 ENVASE 1 LT 10 1
103 2016-01-01 COMIDAS REST 73 NOPAL/PIÑA VASO 34 6
104 2016-01-01 DESAYUNOS REST 73 NOPAL/PIÑA VASO 34 1
105 2016-01-01 DESAYUNOS REST 184 POZ ROJO 85 9
106 2016-01-01 COMIDAS VTSI 693 ENVASE 1 LT 10 1
107 2016-01-01 CENAS REST 184 POZ ROJO 85 2
108 2016-01-01 DESAYUNOS REST 184 POZ ROJO 85 1
我需要的是一个Linq语句,它根据作为我主键的'Pla'数据对每个项目的所有'Quantity'进行求和,我想知道每个Distinct'Pla'有多少'Quantity'并且还将每个不同'Pla'的总和相加,得到如下输出:
Con Fecha Rid Pdv Pla Descripcion Total Quantity
----------------------------------------------------------------------------
1 2016-01-01 COMIDAS FUEM 184 POZ ROJO 1445 17
100 2016-01-01 COMIDAS VTSI 693 ENVASE 1 LT 50 5
103 2016-01-01 COMIDAS REST 73 NOPAL/PIÑA VASO 34 6
104 2016-01-01 DESAYUNOS REST 73 NOPAL/PIÑA VASO 34 1
如何使用Linq语句获取此输出?我所拥有的只是:
foreach (var item in db.Pos.Select(l => l.Pla).Distinct())
{
//Do stuff
}
答案 0 :(得分:4)
您需要使用groupby
db.Pos.GroupBy(a=> a.Pla).Select(p=> new {Pla = p.Key, Quantity = p.Sum(q=>q.Quantity)});
GroupBy将键返回为不同,因此您将获得Pla的不同值以及各自数量的总和
答案 1 :(得分:3)
您可以按GroupBy
值Pla
元素Sum
和Total
组中每个元素的Quantity
和var plas = db.Pos.GroupBy(p => p.Pla).Select(g => new
{
Pla = g.Key,
Total = g.Sum(t => t.Total),
Quantity = g.Sum(t => t.Quantity)
});
值:
:: usage: badpings-color.bat [ip adress | hostname]
@echo off
set /a warnlimit=79 :: greater than this value is red
:: between the two values is yellow
set /a goodlimit=39 :: less than or equal to this value is green
if "%1"=="" (
set pingdest=google.com
) else (
set pingdest=%1
)
echo Pinging %pingdest%.
::echo Logging replies over %limit%ms.
echo Press Ctrl+C to end.
:Loop
for /f "usebackq tokens=1-6" %%a in (`ping -n 1 %pingdest% ^| findstr "Request Reply request"`) do (
set var=%%a %%b %%c %%d %%e %%f
set pingtimestr=%%e
)
if "%pingtimestr%"=="find" (
echo Ping request could not find host %pingdest%. Please check the name and try again.
goto End
)
if "%pingtimestr%"=="host" (
set /a pingtime=%warnlimit%+1
)
if not defined pingtimestr (
set /a pingtime=%warnlimit%+1
)
if "%pingtimestr:~0,4%"=="time" (
set /a pingtime=%pingtimestr:~5,-2%
)
if %pingtime% LEQ %goodlimit% (
call :c 02 "[%time%] %var%" /n
goto EndOfLoop
)
if %pingtime% LEQ %warnlimit% (
call :c 0E "[%time%] %var%" /n
goto EndOfLoop
)
if %pingtime% GTR %warnlimit% (
call :c 0C "[%time%] %var%" /n
goto EndOfLoop
)
:EndOfLoop
timeout /t 1 /nobreak >nul
Goto Loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: this section is from
:: https://stackoverflow.com/questions/4339649/how-to-have-multiple-colors-in-a-windows-batch-file/10407642#10407642
:c
setlocal enableDelayedExpansion
:colorPrint Color Str [/n]
setlocal
set "s=%~2"
call :colorPrintVar %1 s %3
exit /b
:colorPrintVar Color StrVar [/n]
if not defined DEL call :initColorPrint
setlocal enableDelayedExpansion
pushd .
':
cd \
set "s=!%~2!"
:: The single blank line within the following IN() clause is critical - DO NOT REMOVE
for %%n in (^"^
^") do (
set "s=!s:\=%%~n\%%~n!"
set "s=!s:/=%%~n/%%~n!"
set "s=!s::=%%~n:%%~n!"
)
for /f delims^=^ eol^= %%s in ("!s!") do (
if "!" equ "" setlocal disableDelayedExpansion
if %%s==\ (
findstr /a:%~1 "." "\'" nul
<nul set /p "=%DEL%%DEL%%DEL%"
) else if %%s==/ (
findstr /a:%~1 "." "/.\'" nul
<nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%"
) else (
>colorPrint.txt (echo %%s\..\')
findstr /a:%~1 /f:colorPrint.txt "."
<nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
)
)
if /i "%~3"=="/n" echo(
popd
exit /b
:initColorPrint
for /f %%A in ('"prompt $H&for %%B in (1) do rem"') do set "DEL=%%A %%A"
<nul >"%temp%\'" set /p "=."
subst ': "%temp%" >nul
exit /b
:cleanupColorPrint
2>nul del "%temp%\'"
2>nul del "%temp%\colorPrint.txt"
>nul subst ': /d
exit /b
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:End