C# JSON.NET LINQ到实体错误
我不知道如何才能完成这项任务,我尝试了许多方法,但都出现了这样或那样的错误。我尝试过的几种方法没有在我的测试中出错,只是没有提供我想要的结果 我在最终结果中寻找的示例C# JSON.NET LINQ到实体错误,c#,linq-to-entities,json.net,C#,Linq To Entities,Json.net,我不知道如何才能完成这项任务,我尝试了许多方法,但都出现了这样或那样的错误。我尝试过的几种方法没有在我的测试中出错,只是没有提供我想要的结果 我在最终结果中寻找的示例 { 'type': 'NamedAA', 'id': '63c0f27a-716e-804c-6873-cd99b945b63f', 'x': 80, 'y': 59, 'width': 99, 'height': 107, 'userData': { },
{
'type': 'NamedAA',
'id': '63c0f27a-716e-804c-6873-cd99b945b63f',
'x': 80,
'y': 59,
'width': 99,
'height': 107,
'userData': {
},
'cssClass': 'DBTable',
'bgColor': '#DBDDDE',
'color': '#D7D7D7',
'stroke': 1,
'alpha': 1,
'radius': 3,
'name': 'DuringHoursAutoAttendant',
'entities': [
{
'text': 'id',
'id': '49be7d78-4dcf-38ab-3733-b4108701f1'
},
{
'text': 'employee_fk',
'id': '49be7d78-4dcf-38ab-3733-b4108701fce4'
}
]
}
给我错误的代码
var aahope=new JObject(
new JProperty("type", "NamedAA"),
new JProperty("id",aaid),
new JProperty("x",80),
new JProperty("y",59),
new JProperty("width",99),
new JProperty("height",107),
new JProperty("userData",new JObject()),
new JProperty("cssClass", "DBTable"),
new JProperty("bgColor", "#DBDDDE"),
new JProperty("color", "#D7D7D7"),
new JProperty("stroke",1),
new JProperty("alpha",1),
new JProperty("radius",3),
new JProperty("name",""),
new JProperty("entities", new JArray(
(from e in db.HostedVoiceAAKeys
where e.HostedVoiceAAID == aaid.HostedVoiceAAID
select new JObject(
new JProperty("id",e.OptionKey),
new JProperty("text",e.OptionGuid))).ToArray()
))
);
错误消息:
LINQ to实体中只支持无参数构造函数和初始值设定项
描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源
异常详细信息:System.NotSupportedException:LINQ to实体中仅支持无参数构造函数和初始值设定项
不需要处理JObject/JProperty类。只需从这些类创建一个对象
public class Entity
{
public string text { get; set; }
public string id { get; set; }
}
public class RootObject
{
public string type { get; set; }
public string id { get; set; }
public int x { get; set; }
public int y { get; set; }
public int width { get; set; }
public int height { get; set; }
public UserData userData { get; set; }
public string cssClass { get; set; }
public string bgColor { get; set; }
public string color { get; set; }
public int stroke { get; set; }
public int alpha { get; set; }
public int radius { get; set; }
public string name { get; set; }
public List<Entity> entities { get; set; }
}
不需要处理JObject/JProperty类。只需从这些类创建一个对象
public class Entity
{
public string text { get; set; }
public string id { get; set; }
}
public class RootObject
{
public string type { get; set; }
public string id { get; set; }
public int x { get; set; }
public int y { get; set; }
public int width { get; set; }
public int height { get; set; }
public UserData userData { get; set; }
public string cssClass { get; set; }
public string bgColor { get; set; }
public string color { get; set; }
public int stroke { get; set; }
public int alpha { get; set; }
public int radius { get; set; }
public string name { get; set; }
public List<Entity> entities { get; set; }
}
不需要处理JObject/JProperty类。只需从这些类创建一个对象
public class Entity
{
public string text { get; set; }
public string id { get; set; }
}
public class RootObject
{
public string type { get; set; }
public string id { get; set; }
public int x { get; set; }
public int y { get; set; }
public int width { get; set; }
public int height { get; set; }
public UserData userData { get; set; }
public string cssClass { get; set; }
public string bgColor { get; set; }
public string color { get; set; }
public int stroke { get; set; }
public int alpha { get; set; }
public int radius { get; set; }
public string name { get; set; }
public List<Entity> entities { get; set; }
}
不需要处理JObject/JProperty类。只需从这些类创建一个对象
public class Entity
{
public string text { get; set; }
public string id { get; set; }
}
public class RootObject
{
public string type { get; set; }
public string id { get; set; }
public int x { get; set; }
public int y { get; set; }
public int width { get; set; }
public int height { get; set; }
public UserData userData { get; set; }
public string cssClass { get; set; }
public string bgColor { get; set; }
public string color { get; set; }
public int stroke { get; set; }
public int alpha { get; set; }
public int radius { get; set; }
public string name { get; set; }
public List<Entity> entities { get; set; }
}
LINQ to Entities不允许您使用带参数的构造函数实例化
Select()
中的对象
您可以使用ToList()
更改LINQ语句以执行查询,然后使用Select()
将其投影到JObject
s中
new JArray(
(from e in db.HostedVoiceAAKeys
where e.HostedVoiceAAID == aaid.HostedVoiceAAID)
.ToList()
.Select(h => new JObject(
new JProperty("id",e.OptionKey),
new JProperty("text",e.OptionGuid)))
.ToArray()
)
尽管正如L.B.的答案所示,在这里创建自定义类将是首选方法。LINQ to Entities不允许您使用带参数的构造函数实例化
Select()
中的对象
您可以使用ToList()
更改LINQ语句以执行查询,然后使用Select()
将其投影到JObject
s中
new JArray(
(from e in db.HostedVoiceAAKeys
where e.HostedVoiceAAID == aaid.HostedVoiceAAID)
.ToList()
.Select(h => new JObject(
new JProperty("id",e.OptionKey),
new JProperty("text",e.OptionGuid)))
.ToArray()
)
尽管正如L.B.的答案所示,在这里创建自定义类将是首选方法。LINQ to Entities不允许您使用带参数的构造函数实例化
Select()
中的对象
您可以使用ToList()
更改LINQ语句以执行查询,然后使用Select()
将其投影到JObject
s中
new JArray(
(from e in db.HostedVoiceAAKeys
where e.HostedVoiceAAID == aaid.HostedVoiceAAID)
.ToList()
.Select(h => new JObject(
new JProperty("id",e.OptionKey),
new JProperty("text",e.OptionGuid)))
.ToArray()
)
尽管正如L.B.的答案所示,在这里创建自定义类将是首选方法。LINQ to Entities不允许您使用带参数的构造函数实例化
Select()
中的对象
您可以使用ToList()
更改LINQ语句以执行查询,然后使用Select()
将其投影到JObject
s中
new JArray(
(from e in db.HostedVoiceAAKeys
where e.HostedVoiceAAID == aaid.HostedVoiceAAID)
.ToList()
.Select(h => new JObject(
new JProperty("id",e.OptionKey),
new JProperty("text",e.OptionGuid)))
.ToArray()
)
尽管正如L.B.的答案所示,创建自定义类将是这里的首选方法。构造对象。。。听起来怪怪的所以我实际上已经创建了一些类,但是我的问题是如何将实体包含在RootObject中。这就是用一个RootObject向多个实体查询数据库。你的建议虽然听起来很好,但在最终解决了另一个阻止我正确测试的问题后,它缺少了实际拉入数据的功能。我发现这实际上是最好的答案,效果很好。构造一个对象。。。听起来怪怪的所以我实际上已经创建了一些类,但是我的问题是如何将实体包含在RootObject中。这就是用一个RootObject向多个实体查询数据库。你的建议虽然听起来很好,但在最终解决了另一个阻止我正确测试的问题后,它缺少了实际拉入数据的功能。我发现这实际上是最好的答案,效果很好。构造一个对象。。。听起来怪怪的所以我实际上已经创建了一些类,但是我的问题是如何将实体包含在RootObject中。这就是用一个RootObject向多个实体查询数据库。你的建议虽然听起来很好,但在最终解决了另一个阻止我正确测试的问题后,它缺少了实际拉入数据的功能。我发现这实际上是最好的答案,效果很好。构造一个对象。。。听起来怪怪的所以我实际上已经创建了一些类,但是我的问题是如何将实体包含在RootObject中。这就是用一个RootObject向多个实体查询数据库。你在这里的建议虽然听起来不错,但在最终解决了另一个阻止我正确测试的问题后,它缺少了实际拉入数据的功能。我发现这实际上是最好的答案,效果很好。这个解决方案实际上对我来说是错误的非静态方法需要一个目标。描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。异常详细信息:System.Reflection.TargetException:非静态方法需要一个目标。因此,使用EF研究该错误消息后,它似乎总是与空值相关,我会仔细检查我的结果,让你知道结果我没有专门测试这个解决方案,因为L.B的似乎工作得很好,现在我修复了我得到的错误,这与你们建议的代码无关。现在也许我可以继续前进,让这个项目继续下去。顺便说一句,我收到的错误消息是由于where子句中的空值造成的。FacePalm此解决方案实际上在me上出错非静态方法需要一个目标。描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。异常详细信息:System.Reflection.TargetException:非静态方法需要目标