sql到linq查询解决方案 [英] Sql to linq query solution
问题描述
我首先尝试从一个表中获取数据我创建sql查询然后我尝试将此sql转换为linq查询
我尝试了什么:
[WebMethod]
public static string GetVol()
{
尝试
{
TrackDataEntities1 DB = new TrackDataEntities1();
var a = DB.tblVeh;
string data = [跨度>;
foreach ( var p in a)
{
var re =( from vehvoila 在 DB.tblVeh
其中 vehvoila.MID.Equals( 23065)&&!(vehvoila.Name == )
group vehvoila by new { vehvoila.Name} into g
选择 new
{
g.Key.Name,
cnt = g.Select(t = > t.Name ).Count()
});
}
data = data.Substring( 0 ,data.Length - 1 跨度>);
data + = ];
返回数据;
}
catch (异常异常)
{
throw new Exception();
}
}
实际的sql查询是
选择名称,计数(*)来自 tblVeh
WHERE MID = 23065 和名称<> ' ' 组 按名称
数据
VName(无列名)
d1 2
s2 1
f3 2
现在当我调试`'re'`显示
'System.Data.Objects.ObjectQuery<<> f__AnonymousType1< string,int>>}'
但是我想要这样的数据
['d1',2],['s2',1],['f3',20]
i仅从一个表中获取数据
如果要返回一组记录,请尝试:
string sMid = 23065 ;
var re = DB.tblVeh
.Where(v => v.MID.Equals(sMid)&& v.Name != string .Empty)
.GroupBy(v => v.Name)
。选择(g = > new
{
Vname = g.Key.Name,
Count = g。 Count()
})。ToList();
以上代码返回:
< pre lang =text> Vname计数
d1 2
e3 5
g2 8
为了能够完成结果集,你必须使用foreach(...)
循环:
Console.WriteLine( Vname - Count);
foreach ( var a in re)
{
Console.WriteLine( {0} - {1},a.Vname,a.Count);
}
如果你想返回用逗号分割的字符串,试试这个:
< span class =code-keyword> string sMid = 23065;
var re1 = String .Join( ,,DB.tblVeh
.Where(v => v.MID.Equals(sMid)&& v.Name != string .Empty)
.GroupBy(v => v.Name)
。选择(g = > new
{
a = String .Format( [{0},{1}],g.Key.Name,g .Count())
}));
以上代码返回:
[d1 ,2],[e3,5],[g2,8]
i try to fetch data from one table first i create sql query and then i try to convert this sql to linq query
What I have tried:
[WebMethod]
public static string GetVol()
{
try
{
TrackDataEntities1 DB = new TrackDataEntities1();
var a = DB.tblVeh;
string data = "[";
foreach (var p in a)
{
var re = (from vehvoila in DB.tblVeh
where vehvoila.MID.Equals("23065") && !(vehvoila.Name == "")
group vehvoila by new { vehvoila.Name } into g
select new
{
g.Key.Name ,
cnt = g.Select(t => t.Name ).Count()
});
}
data = data.Substring(0, data.Length - 1);
data += "]";
return data;
}
catch (Exception exception)
{
throw new Exception();
}
}
actual sql query is
Select Name, count(*) from tblVeh
WHERE MID = 23065 and Name <> '' Group By Name
data
VName (No column name)
d1 2
s2 1
f3 2
now when i debug `'re'` shows
'System.Data.Objects.ObjectQuery<<>f__AnonymousType1<string,int>>}'
but i want data like this
['d1',2],['s2',1],['f3',20]
i fetch data only from one table
If you want to return a set of records, try this:
string sMid = "23065"; var re = DB.tblVeh .Where(v=>v.MID.Equals(sMid) && v.Name != string.Empty) .GroupBy(v=>v.Name) .Select(g=> new { Vname = g.Key.Name, Count = g.Count() }).ToList();
Above code returns:
Vname Count d1 2 e3 5 g2 8
To be able to go through the resultset, you have to useforeach(...)
loop:
Console.WriteLine("Vname - Count"); foreach(var a in re) { Console.WriteLine("{0} - {1}", a.Vname, a.Count); }
If you want to return string splited by comma, try this:
string sMid = "23065"; var re1 = String.Join(",", DB.tblVeh .Where(v=>v.MID.Equals(sMid) && v.Name != string.Empty) .GroupBy(v=>v.Name) .Select(g=> new { a = String.Format("[{0},{1}]", g.Key.Name, g.Count()) }));
Above code returns:
[d1,2],[e3,5],[g2,8]
这篇关于sql到linq查询解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!