将查询转换为linq [英] convert query to linq

查看:107
本文介绍了将查询转换为linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将此查询转换为linq



选择年份(CreatedAt),从会员计算(会员ID)

其中ID = 1

按年份分组(CreatedAt)

How to convert this query to linq

select year(CreatedAt),count(MemberID) from Members
where ID = 1
group by year(CreatedAt)

推荐答案

这真的不是那么难。努力奋斗。

我建议你下载 LinqPad [ ^ ],您可以使用它来在线测试您的查询,并使用linq来解决任何问题。顺便说一下,它包含一组示例和教程 - 其中有几个可以帮助您解决此任务。
This is really not that hard. Struggle a little bit.
I suggest you download LinqPad[^], which you can use to test your queries online, and to play around with linq to anything. And by the way it contains a set of samples and tutorials - among which you will find several that will help you in solving this task.




我的建议是,如果你经常使用linq查询,

使用下面的链接并安装sql链接转换器,

即使我使用相同的..

http://www.linqpad.net/ [ ^ ]



它很简单,你可以在sql中构建任何复杂的查询然后在linqpad中复制粘贴然后在按钮单击中你会得到一个linq查询。

即使没有Linq的熟练知识,我已经完成了一个使用Linq的整个项目:





祝你好运..
Hi,
my advice is if you frequently using linq queries then,
use the below link and install sql to link converter,
even i use the same..
http://www.linqpad.net/[^]

it is so easy and you can just build any complex query in sql and then copy paste in linqpad then in a button click you will get a linq query.
even without having sound knowledge on Linq, i have completed a whole project which uses Linq :)


Good luck..


嗨dineshdena,



不要忘记

Hi dineshdena,

Don't forget
using System.Linq;



否则你ca不要使用LINQ扩展方法,你需要创建存储数据的类。

这是代码:


otherwise you can't use neither LINQ extension method and you need to create the class to store the data.
Here is the Code:

[DataContract]
   public class MemberModel
   {
       [DataMember]
       public string year{ get; set; }
       [DataMember]
       public string Count{ get; set; }
   }




var dataQuery =
    from Mem in Members
    where Mem.ID == 1
    group Mem by new { Mem.year(CreatedAt) } into g
    select new MemberModel()
    {
        year = g.Key.year(CreatedAt), 
        Count= g.Key.count(MemberID)
    };


这篇关于将查询转换为linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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