我想在SQL
数据网格中显示此WPF
查询的结果。我的问题如何使用EF6运行此查询?
select c.*, count(o.id) from clients c
left join optics o
on o.client_id = c.id
where o.r_sph = 12 // 12 is a variable value
group by c.id
我的客户模型:
public clients()
{
this.optics = new ObservableCollection<optics>();
}
public long id { get; set; }
public string name { get; set; }
public string phone { get; set; }
public Nullable<System.DateTime> date_of_birth { get; set; }
public System.DateTime inserted_at { get; set; }
public System.DateTime updated_at { get; set; }
public virtual ObservableCollection<optics> optics { get; set; }
和光学模型:
public long id { get; set; }
public Nullable<System.DateTime> at { get; set; }
public Nullable<decimal> r_sph { get; set; }
public Nullable<int> r_axs { get; set; }
public string notes { get; set; }
public Nullable<long> client_id { get; set; }
public System.DateTime inserted_at { get; set; }
public System.DateTime updated_at { get; set; }
public virtual clients clients { get; set; }
更新
我使用此代码过滤结果:
public IQueryable < Multi.Model.clients > FilterClients(optisysEntities db, System.Linq.IQueryable < Multi.Model.clients > clients, ClientsFilter clientsFilter) {
if (!String.IsNullOrWhiteSpace(clientsFilter.Name))
clients = db.clients.Where(c => c.name.Contains(clientsFilter.Name));
if (!String.IsNullOrWhiteSpace(clientsFilter.Phone))
clients = clients.Where(u => u.phone.Contains(clientsFilter.Phone));
}
答案 0 :(得分:1)
您需要返回一个新对象(除非您已经有一个包含“Count”属性的模型设置)。像这样:
var query = (from c in context.Clients
join o in context.Optics
on c.id equals o.client_id
where o.r_sph == 12
group new { c, o } by new { c.Id }
into g
select new {Id = g.Key.id,
// other properties from "Client",
Count = g.Count()}
).ToList();