class Entity {
public MyEnum Type { get; set; }
public bool IsEnabled { get; set; }
}
class FilterItem {
public string Name { get; set; }
public int Total { get; set; }
}
IQueryable<Entity> entityQuery = _db.Entities;
IQueryable<IGrouping<string , Entity>> groupingQuery;
groupingQuery = entityQuery.GroupBy(f => f.Type);
var query = groupingQuery.Select(f => new FilterItem
{
Name = f.Key, // Here the type error occurs
Total = f.Count()
});
return query.ToListAsync();
现在,它抛出一个错误,它无法将MyEnum转换为字符串。因此,我尝试这样做:
groupingQuery = entityQuery.GroupBy(f => f.Type.ToString()); // Cannot call a function like that in a query builder.
但是我发现实际上尝试运行此部分时是不可能的。我的问题是:
是否可以转换枚举,使其与声明的“ groupingQuery”变量类型匹配?
或者,如果'groupingQuery'的类型为
IQueryable<IGrouping<MyEnum , BucketEntity>>
那么也许可以通过Select进行转换?
var query = groupingQuery.Select(f => new FilterItem
{
Name = f.Key.toString(), // This is what I would imagine in an ideal world
Total = f.Count()
});
答案 0 :(得分:1)
您实际上可以呼叫键上的Select()
。但是,仅在GroupBy()
中而不在ToString
中。观看toString
(不是class Program
{
static void Main(string[] args)
{
var q = new Test[] { Test.One, Test.Two, Test.Two };
var e = q.AsQueryable().GroupBy(x => x).Select(x => new
{
Name = x.Key.ToString(),
Amount = x.Count()
});
foreach (var x in e)
{
Console.WriteLine(x.Name);
}
Console.ReadLine();
}
}
enum Test
{
One,
Two
}
)的写作,它应该可以工作。
我用以下代码对其进行了测试:
One
Two
程序的输出为:
$(function () {
$("[id=textboxDOFE]").change(function () {
var DOFE = $(this).val();
var CaseID = $('[id=CaseID]').val();
$.ajax({
type: "POST",
url: "Case.aspx/DOFE_update",
data: '{"CaseID":' + CaseID + ', "DOFE":"' + DOFE + '"}',
contentType: "application/json;charset=utf-8",
dataType: "json",
async: false,
error: function (request) {
alert(request.responseText);
},
success: function (response) {
alert("success");
}
});
});
});