如何在模型绑定中使用 Group By 查询
How to use Group By query in Model Binding
在我的 ASP.NET Web 表单应用程序中,我使用(模型绑定/Entity Framework)。
我想在 Gridview 中显示我的 Linq 查询(分组依据)的结果,但我不能!
<asp:GridView runat="server" ID="UsersGrid"
ItemType="myApp.Models.users" DataKeyNames="id"
SelectMethod="UsersGrid_GetData"
AutoGenerateColumns="false">
<Columns>
<asp:DynamicField DataField="Fonction" />
<asp:DynamicField DataField="Count" />
</Columns>
</asp:GridView>
隐藏代码:
public IQueryable<users> UsersGrid_GetData()
{
ModelData db = new ModelData();
var result = from d in db.users
group d by d.Fonction into grouped
select new
{
Fonction = grouped.Key,
Count = grouped.Count()
};
return result.AsQueryable();
}
我有这个错误:
Cannot implicitly convert type 'System.Linq.IQueryable<>' to
'System.Linq.IQueryable'. An explicit
conversion exists (are you missing a cast?)
结果不是 Model
的 IQueryable
,而是 Anonymous Type
的 IQueryable
。所以它应该是这样的(创建一个新的 DTO
class 和 return 代替):
public IQueryable<DTO> UsersGrid_GetData()
{
ModelData db = new ModelData();
var result = from d in db.users
group d by d.Fonction into grouped
select new DTO
{
Fonction = grouped.Key,
Count = grouped.Count()
};
return result.AsQueryable();
}
public class DTO
{
public string Fonction { get; set; }
public int Count { get; set; }
}
您正在创建匿名类型的对象并试图将其转换为可查询类型。这就是您收到此错误的原因。
试试这个:
方法 1:如果您想使用用户模型本身,其中 'users' 具有您需要绑定到网格视图的属性。
public IQueryable<users> UsersGrid_GetData()
{
ModelData db = new ModelData();
var result = from d in db.users
group d by d.Fonction into grouped
select new users
{
Fonction = grouped.Key,
Count = grouped.Count()
};
return result;
}
方法二:使用不同的模型'p'
public IQueryable<p> UsersGrid_GetData()
{
ModelData db = new ModelData();
var result = from d in db.users
group d by d.Fonction into grouped
select new p
{
Fonction = grouped.Key,
Count = grouped.Count()
};
return result;
}
public class p
{
public int? Fonction { get; set; }
public int Count { get; set; }
}
而不是 'p' 根据您的应用程序提供有效名称。同时在网格视图中进行必要的更改(您可能必须更改“ ItemType="myApp.Models.users")。
在我的 ASP.NET Web 表单应用程序中,我使用(模型绑定/Entity Framework)。
我想在 Gridview 中显示我的 Linq 查询(分组依据)的结果,但我不能!
<asp:GridView runat="server" ID="UsersGrid"
ItemType="myApp.Models.users" DataKeyNames="id"
SelectMethod="UsersGrid_GetData"
AutoGenerateColumns="false">
<Columns>
<asp:DynamicField DataField="Fonction" />
<asp:DynamicField DataField="Count" />
</Columns>
</asp:GridView>
隐藏代码:
public IQueryable<users> UsersGrid_GetData()
{
ModelData db = new ModelData();
var result = from d in db.users
group d by d.Fonction into grouped
select new
{
Fonction = grouped.Key,
Count = grouped.Count()
};
return result.AsQueryable();
}
我有这个错误:
Cannot implicitly convert type 'System.Linq.IQueryable<>' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?)
结果不是 Model
的 IQueryable
,而是 Anonymous Type
的 IQueryable
。所以它应该是这样的(创建一个新的 DTO
class 和 return 代替):
public IQueryable<DTO> UsersGrid_GetData()
{
ModelData db = new ModelData();
var result = from d in db.users
group d by d.Fonction into grouped
select new DTO
{
Fonction = grouped.Key,
Count = grouped.Count()
};
return result.AsQueryable();
}
public class DTO
{
public string Fonction { get; set; }
public int Count { get; set; }
}
您正在创建匿名类型的对象并试图将其转换为可查询类型。这就是您收到此错误的原因。
试试这个: 方法 1:如果您想使用用户模型本身,其中 'users' 具有您需要绑定到网格视图的属性。
public IQueryable<users> UsersGrid_GetData()
{
ModelData db = new ModelData();
var result = from d in db.users
group d by d.Fonction into grouped
select new users
{
Fonction = grouped.Key,
Count = grouped.Count()
};
return result;
}
方法二:使用不同的模型'p'
public IQueryable<p> UsersGrid_GetData()
{
ModelData db = new ModelData();
var result = from d in db.users
group d by d.Fonction into grouped
select new p
{
Fonction = grouped.Key,
Count = grouped.Count()
};
return result;
}
public class p
{
public int? Fonction { get; set; }
public int Count { get; set; }
}
而不是 'p' 根据您的应用程序提供有效名称。同时在网格视图中进行必要的更改(您可能必须更改“ ItemType="myApp.Models.users")。