简体   繁体   English

将SQL查询转换为LINQ

[英]Convert SQL query to LINQ

I have a table called visit with the following columns: 我有一个名为visit的表,其中包含以下列:

visit_Id
member_Id 
visit_Date 
visit_Time 
visit_DateTime 
visit_Status values like (accepted, refused)

I have the following SQL query: 我有以下SQL查询:

string sql = @"SELECT CONCAT(UPPER(SUBSTRING(visit_Status, 1, 1)), SUBSTRING(visit_Status FROM 2))  as Status, COUNT('x') AS Visits
   FROM visits
   WHERE visit_Date BETWEEN '2001-09-08' AND '2009-09-09'
   GROUP BY visit_Status";

How can I convert this SQL into LINQ? 如何将该SQL转换为LINQ? My entity name is dbcontext . 我的实体名称是dbcontext Thanks in advance for any help. 在此先感谢您的帮助。

You need to use EntityFunctions 您需要使用EntityFunctions

DateTime dateFrom = new DateTime(2001, 9, 8);
DateTime dateTo = new DateTime(2001, 9, 9);

var query = from v in dbcontext.Visits
            where v.visit_Date >= dateFrom && v.visit_Date <= dateTo
            group v by v.visit_Status into vg
            select new
            {
              Status = EntityFunctions.Concat(EntityFunctions.ToUpper(vg.Key[0]),
                                              EntityFunctions.SubString(1, EntityFunctions.Length(vg.Key) -1),
              Visits = vg.Count()
            }

Can you tell me what you are trying to do with Count('x')? 你能告诉我你想用Count('x')做什么吗?

from v in dbcontext.visits
where v.visit_Date >= "2001-09-08" && v.visitDate <= "2009-09-09"
group by v.visit_Status
select new
{
    Status = string.Concat(char.ToUpper(v.visit_Status[0], v.visit_Status.Substring(1)),
    Visits = //Not sure what 'x' is in your example
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM