using System.Data.Entity;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
public static void Main()
using (var db = new EntityContext())
using (var db = new EntityContext())
DumpTable(db.Hato, "All HATO Records");
DateTime startDate = new DateTime(2021,05,21);
DateTime endDate = new DateTime(2021,05,24);
DumpTable(db.Hato.Where(L => L.HatoRecDate >= startDate && L.HatoRecDate <= endDate), String.Format("HATO Between [{0:dd.MM.yyyy} - {1:dd.MM.yyyy}]", startDate, endDate));
where L.HatoRecDate >= startDate && L.HatoRecDate <= endDate
group L by L.HatoLocation into g
select new { HatoLocation = g.Key, count = g.Count() })
.OrderByDescending(o => o.count);
Console.WriteLine("Original Query SQL:");
Console.WriteLine(qLocationOriginal.ToString());
var qLocationQuery = from L in db.Hato
where L.HatoRecDate >= startDate && L.HatoRecDate <= endDate
group L by L.HatoLocation into g
select new { HatoLocation = g.Key, count = g.Count() };
Console.WriteLine("Hato Query SQL:");
Console.WriteLine(qLocationQuery.ToString());
var qLocation = qLocationQuery.ToList();
qLocation = qLocation.OrderByDescending(o => o.count).ToList();
DumpTable(qLocation, "qLocation Grouping Results");
var l = qLocation[0].HatoLocation;
var c = qLocation[0].count;
Console.WriteLine("qLocation[0].HatoLocation: {0}", l);
Console.WriteLine("qLocation[0].count: {0}", c);
public class EntityContext : DbContext
public EntityContext() : base(FiddleHelper.GetConnectionStringSqlServer())
public DbSet<Hato> Hato { get; set; }
Hato.Add(new Hato { HatoRecDate = new DateTime(2021,05,21), HatoLocation = "A" });
Hato.Add(new Hato { HatoRecDate = new DateTime(2021,05,21), HatoLocation = "A" });
Hato.Add(new Hato { HatoRecDate = new DateTime(2021,05,23), HatoLocation = "B" });
Hato.Add(new Hato { HatoRecDate = new DateTime(2021,05,24), HatoLocation = "A" });
Hato.Add(new Hato { HatoRecDate = new DateTime(2021,05,24), HatoLocation = "B" });
Hato.Add(new Hato { HatoRecDate = new DateTime(2021,05,20), HatoLocation = "A" });
Hato.Add(new Hato { HatoRecDate = new DateTime(2021,05,25), HatoLocation = "B" });
public int Id { get; set; }
public DateTime HatoRecDate { get;set; }
public string HatoLocation { get;set; }
public static void DumpTable<T>(IEnumerable<T> table, string header = null)
if(!String.IsNullOrWhiteSpace(header))
Console.WriteLine("// {0}", header);
var columns = typeof(T).GetProperties();
Console.WriteLine("|{0}|", String.Join("|", columns.Select(c => c.Name)));
Console.WriteLine("|{0}|", String.Join("|", columns.Select(c => new String('-', c.Name.Length))));
foreach (T record in table)
foreach (var c in columns)
Console.Write("{0}|", c.GetValue(record, null));