select count(tblVV.VNme) as total,
tblvV.VNme
from tblVV
inner join tblRV
on tblVV.MID=tblRV.ID
inner join tblRe
on tblRV.RID=tblRe.RID
where tblRe.StartDate>= '2016-07-01 00:00:00' and
tblRe.EndDate<= '2016-07-31 23:59:59' and
tblRe.Reg= 'uk' and
tblRV.RegNo='BR72' and
tblVV.VNme <>''
group by tblVV.VNme
对于上述查询,我得到:
total Vame
1 DDSB
11 MV
上面的SQL查询显示了正确的数据,所以现在我尝试将上面的查询转换为linq查询
[WebMethod]
public static string GetVo(string RegNo)
{
string data = "[";
try
{
Ts1 DB = new Ts1();
var re = (from vehvoila in DB.tblVV
join regveh in DB.tblRV on vehvoila.MID equals regveh.ID
join reg in DB.tblReg on regveh.RID equals reg.RID
where regveh.RegNo == RegNo &&
vehvoila.Vame != ""
group vehvoila by vehvoila.Vame into g
select new
{
VNme = g.Key,
cnt = g.Select(t => t.Vame).Count()
}).ToList();
if (re.Any())
{
data += re.ToList().Select(x => "['" + x.Vame + "'," + x.cnt + "]")
.Aggregate((a, b) => a + "," + b);
}
data += "]";
}
linq查询显示我返回这样的数据
[['DDSB',1],['DPSB',1],['DSB',109],['MV',39],['PSB',1]]
我想要数据
[['DDSB',1],['MV',11]]
现在返回SQL查询的数据是正确的,所以我如何更正linq查询
注意:忘记SQL查询中的fromdate,todate,region参数。因为我有一个页面,其中我放下拉列表和日期和todate选择器,并有按钮,所以当我选择值,即英国,日期然后数据显示在表格中,然后当我点击表格中的任何一行,然后我想得到这些数据在data +=”]”;
实际上是在点击行
背后的linq查询工作之上total Vame
1 DDSB
11 MV