Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法转换IQueryable<;分组>;到IQueryable<;模型>;_C#_Linq - Fatal编程技术网

C# 无法转换IQueryable<;分组>;到IQueryable<;模型>;

C# 无法转换IQueryable<;分组>;到IQueryable<;模型>;,c#,linq,C#,Linq,我被这个问题困住了。我正在尝试返回我的模型(组)的IQueryable列表。然后我将返回到视图并在下拉框中显示。我让它工作了。但是,当我添加GroupBy时,我收到以下错误: Error 2 Cannot implicitly convert type 'System.Linq.IQueryable<System.Linq.IGrouping<int,Entities.Group>>' to 'System.Linq.IQueryable<Entities.

我被这个问题困住了。我正在尝试返回我的模型(组)的IQueryable列表。然后我将返回到视图并在下拉框中显示。我让它工作了。但是,当我添加GroupBy时,我收到以下错误:

Error   2   Cannot implicitly convert type 'System.Linq.IQueryable<System.Linq.IGrouping<int,Entities.Group>>' to 'System.Linq.IQueryable<Entities.Group>'. An explicit conversion exists (are you missing a cast?) 
林克:

公共类SQLSupplierGroupRepository:ISupplierGroupRepository{
ApplicationDbContext Db=新的ApplicationDbContext();
公共IQueryable GetAllSupplierClaimGroupsByClient(int-ClientID){
返回(来自数据库组中的sg)
其中sg.ClientID==ClientID
选择sg);
}
公共空间处置(){
if(camOnlineDb!=null){
camOnlineDb.Dispose();
}
}
}
我知道我可以在控制器或LINQ语句中进行订购。然而,我尝试了这个,OrderBy似乎对结果没有影响

编辑:

我在LINQ中尝试了这一点,但这似乎对结果的顺序没有影响:

public IQueryable<Group> GetAllSupplierClaimGroupsByClient(int ClientID) {
            var x =  (from sg in Db.Group
                     where sg.ClientID == ClientID
                     select sg);

            x.OrderBy(r => r.GroupName);
            x.GroupBy(r => r.GroupID);
            x.Select(g => new Group {
                GroupID = g.GroupID,
                GroupName = g.GroupName
            });


            return x;
        }
public IQueryable GetAllSupplierClaimGroupsByClient(int-ClientID){
变量x=(来自数据库组中的sg
其中sg.ClientID==ClientID
选择sg);
x、 OrderBy(r=>r.GroupName);
x、 GroupBy(r=>r.GroupID);
x、 选择(g=>newgroup{
GroupID=g.GroupID,
GroupName=g.GroupName
});
返回x;
}

我不得不向一位同事寻求这方面的帮助,我的问题是双重的。但我只问了一个问题。结果如下:

 public IEnumerable<GroupModel> GetAllSupplierClaimGroupsByClientGrouping(int ClientID) {

            var x = (from g in camOnlineDb.Group
                     where g.ClientID == 1
                     group g by new { GroupID = g.GroupID, GroupName = g.GroupName }
                         into a
                         select new GroupModel{ GroupID = a.Key.GroupID, GroupName = a.Key.GroupName });
            return x.OrderBy( r=> r.GroupName);
        }
public IEnumerable GetAllSupplierClaimGroupsByClientGrouping(int-ClientID){
var x=(来自camOnlineDb.Group中的g
其中g.ClientID==1
按新{GroupID=g.GroupID,GroupName=g.GroupName}分组g
变成
选择新的组模型{GroupID=a.Key.GroupID,GroupName=a.Key.GroupName});
返回x.OrderBy(r=>r.GroupName);
}

您需要使用
选择
来投影输出,以返回
IQueryable
。目前它正在返回
IQueryableI我已经尝试过了,但没有成功。我将根据您的编辑添加一个编辑以显示您,首先您不能这样做:
GroupID=g.GroupID
,而是需要执行
GroupID=g.Key
。其次,对于获取
GroupName
,您需要指定需要从每个组中获取的组名,因为
g
将在每个组ID下包含一个
GroupName
列表。除此之外,为什么混合使用查询和方法语法?您可以通过链接不同的方法来完成所有这些。如果您看到我最后的评论,这正是我建议您的:)基于分组键投影您的模型。我注意到了。谢谢你的帮助!
public IQueryable<Group> GetAllSupplierClaimGroupsByClient(int ClientID) {
            var x =  (from sg in Db.Group
                     where sg.ClientID == ClientID
                     select sg);

            x.OrderBy(r => r.GroupName);
            x.GroupBy(r => r.GroupID);
            x.Select(g => new Group {
                GroupID = g.GroupID,
                GroupName = g.GroupName
            });


            return x;
        }
 public IEnumerable<GroupModel> GetAllSupplierClaimGroupsByClientGrouping(int ClientID) {

            var x = (from g in camOnlineDb.Group
                     where g.ClientID == 1
                     group g by new { GroupID = g.GroupID, GroupName = g.GroupName }
                         into a
                         select new GroupModel{ GroupID = a.Key.GroupID, GroupName = a.Key.GroupName });
            return x.OrderBy( r=> r.GroupName);
        }