C#中的LINQ中的SQL查询 [英] SQL Query in LINQ in C#

查看:75
本文介绍了C#中的LINQ中的SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我需要LINQ中的等效查询.谁能帮我吗?

Hello,

I need the equivalent query in LINQ. Can any one please help me ?

Select count(*),Name 
from Test
where convert(varchar(12),postdate,101) =  convert(varchar(12),GETDATE(),101)
group by Name




谢谢.




Thank you.

推荐答案

这是我的解决方案版本.


This is my version of the solution.


 var tomorrow = DateTime.Now.Date.AddDays(1);
 var today = DateTime.Now.Date;
 
 var ts = from t in Tests
     where t.postdate >= today && t.postdate < tomorrow
     group t by t.Name into newt
     select new { Name = newt.Key, Count = newt.Count() };
	 
foreach ( var t in ts) {
	Console.WriteLine("{0} {1}",t.Count, t.Name);
}



它查找今天发布的测试条目,并按名称分组后进行计数.



It finds the Test entries that were posted today and counts after grouping by name.


DataClasses1DataContext dc = new DataClasses1DataContext();

   var query = from c in dc.Test
        where Convert.ToString(postdate) == Convert.ToString(DateTime.Now.Date)
        group c by c.Name into g
        select new  { Count = g.Count() , Name = g.Key  };



我刚刚测试过,此代码有效.



I just tested, this code works.


我认为这或多或少是等效的:

I think this would be the more or less equivalent:

from test in testcollection
where test.postdate == DateTime.Now.Date
group test by test.name into newtest
select new { Name = newtest.Name, PostDate = newtest.postdate };


这篇关于C#中的LINQ中的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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