简体   繁体   English

如何在LINQ中编写以下查询

[英]How to write the following query in LINQ

I have a sql query and would like to convert it into linq 我有一个SQL查询,想将其转换为linq

 SELECT CAST([Date] AS DATE),
 COUNT([ID]) AS 'Amount of Systems'
 FROM [DemoDB].[dbo].[Servers]
 WHERE [ServerID] IN ('ServerX') AND [Type] = 'Complete'  
 GROUP BY CAST([Date] AS DATE)
 ORDER BY CAST([Date] AS DATE) 

this will return the result as follows 这将返回结果如下

在此处输入图片说明

What I have tried 我尝试过的

 //fromDP and toDP are the names of the Datepicker's
 var query = (this.db.Servers
                     .Where(x => x.Date >= fromDP.SelectedDate.Value && 
                     x.Date <= toDP.SelectedDate.Value));


 var query_Success = query.Count(p => p.Type == "Complete" 
                     && (p.ServerID == "ServerX"));     

and I have the result as Count on the whole ( for example, if I select from from April 1st to April 15th , the result is the sum of all "complete"), but I need count for each day in this selected range. 并且我的结果总体上为Count(例如,如果我从4月1日到4月15日进行选择,则结果为所有“完成”的总和),但是我需要在此选定范围内每天进行计数。 the result I will bind to the column chart. 结果我将绑定到柱形图。 how to proceed ? 如何进行 ?

this.db.Servers.Where(s => s.ServerId == "ServerX" && s.Type == "Complete")
       .GroupBy(s => s.Date)
       .OrderBy(s => s.Key)
       .Select(g => new { Date = g.Key, AmountOfSystems = g.Count() });

Change the Where clause to read 将Where子句更改为

Where(s => s.ServerId == "ServerX" && s.Type == "Complete" && s.Date >= fromDP.SelectedDate.Value && s.Date <= toDP.SelectedDate.Value)

to filter to a limited date range. 过滤到有限的日期范围。

EDIT 编辑

As @vvs0205 suggested. 如@ vvs0205建议。 Use EntityFunctions class to manipulate the date column as you please: http://msdn.microsoft.com/en-us/library/system.data.objects.entityfunctions.aspx 使用EntityFunctions类来按需操纵日期列: http : //msdn.microsoft.com/zh-cn/library/system.data.objects.entityfunctions.aspx

If I understood correctly the author wants to use only the date without the time. 如果我理解正确,那么作者只想使用日期而不使用时间。 To do this with EF we can use the method EntityFunctions.TruncateTime for trimming the time portion. 为此,我们可以使用EntityFunctions.TruncateTime方法来修剪时间部分。 I will build on @steaks answer: 我将基于@steaks答案:

db.Servers.Where(s => s.ServerId == "ServerX" && s.Type == "Complete")
            .GroupBy(s => EntityFunctions.TruncateTime(s.Date))
            .OrderBy(s => s.Key)
            .Select(g => new {Date = g.Key, AmountOfSystems = g.Count()});

Something like this 像这样

var fromDate = fromDP.SelectedDate.Value;
var toDate= toDP.SelectedDate.Value;

var q = from server in this.db.Servers
                where (server.Date >= fromDate && server.Date<=toDate && server.ServerID="ServerX" && server.Type=="Complete")
                group server  by server.Date
                into g
                orderby g.Key
                select new
                    {
                        Date = g.Key,
                        Count = g.Count()
                    };

var results = q.ToList();

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

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