简体   繁体   English

linq 为 C# 应用程序选择不同和计数

[英]linq with select distinct and count for the C# application

I have two sql query, I have already tested both queries are working.我有两个 sql 查询,我已经测试过两个查询都在工作。 What I need to convert to Linq query for the dropdown list(MVC C#).我需要转换为下拉列表的 Linq 查询(MVC C#)。 Are there any way to Linq query should display only date without time?有什么方法可以让 Linq 查询只显示日期而不显示时间? Eg only date 2015-09-30例如仅日期 2015-09-30

  SELECT DISTINCT DelDate  from Location where    
  DStatus = 'true' and FState = 'true' 

output输出

2015-09-30 14:06:37.000
2015-09-30 14:14:09.547

My second query, I need to convert linq list only我的第二个查询,我只需要转换 linq 列表

SELECT COUNT(DISTINCT CouName) AS StatusCount  FROM Location  where   
DlStatus = 'true' and FState = 'true'  and CouName = 'Alan'

As per the SQL in your question, equivalent Linq will be根据您问题中的SQL ,等效的Linq将是

SELECT DISTINCT DelDate from Location where DStatus = 'true' and FState = 'true' SELECT DISTINCT DelDate from Location where DStatus = 'true' and FState = 'true'

var delDates = Location
  .Where(l => l.DStatus == "true" && l.FState == "true")
  .Select(f => f.DelDate)
  .Distinct();

SELECT COUNT(DISTINCT CouName) AS StatusCount FROM Location where DlStatus = 'true' and FState = 'true' and CouName = 'Alan' SELECT COUNT(DISTINCT CouName) AS StatusCount FROM Location where DlStatus = 'true' and FState = 'true' and CouName = 'Alan'

var statusCount = Location
  .Where(l => l.DStatus == "true" && l.FState == "true" && l.CouName == "Alan")
  .Select(f => f.CouName)
  .Distinct()
  .Count();

This这个

db.Location.Where(x=>x.DStatus == "true" && x.FState == "true")
           .Select(y=>y.DelDate.Value.ToString("yyyy-MM-dd")).Distinct().ToList();

and this和这个

    db.Location.Where(x=>x.DStatus == "true" && x.FState == "true" && x.CouName == "Alan")
               .Select(y=>y.CouName).Distinct().Count();

for first query to get only date you can truncate time at selection .Date will get only Date and remove Time portion.对于仅获取日期的第一个查询,您可以在选择时截断时间.Date将仅获取日期并删除时间部分。

var dates = Location.Where(l => l.DStatus && l.FState)
                    .Select(f => f.DelDate.Date)
                    .Distinct();

for second query对于第二个查询

var count = Location.Where(l => l.DStatus && l.FState && l.CouName == "Alan")
                    .Select(f => f.CouName)
                    .Distinct()
                    .Count();

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

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