这是我从API接收的模型。
var query = FindBy(item => item.TaskEnabledStatus == trueOrFalse).Select(items => new MainGridDto()
{
PK_Task = items.PK_Task,TaskName = items.TaskName,TaskDescription = items.TaskDescription,TaskEnabledStatus = items.TaskEnabledStatus,Businessname = items.tBusinessGroups.Businessname,ExecutionTime = items.ExecutionTime.Value,BusinessGroupDto = new BusinessGroupDto()
{
PK_BusinessGroup = items.tBusinessGroups.PK_BusinessGroup,BusinessGroupEnabledStatus = items.tBusinessGroups.BusinessGroupEnabledStatus
},EmailBody = items.EmailBody,EmailSubject = items.EmailSubject,UserOwner = items.tUsers.tRecipients.FirstName + " " + items.tUsers.tRecipients.LastName,UsersDto = new UsersDto()
{
PK_User = items.tUsers.PK_User,username = items.tUsers.username,FirstAndLast = items.tUsers.tRecipients.FirstName + " " + items.tUsers.tRecipients.LastName
},UserLastModified = items.tUsers.username,Frequency = items.Frequency,LastModifiedOn = items.LastModifiedOn
});
我想做的是将对象反序列化为字典,但是由于键为空,所以我一直报错。如何将PK分配给字典并将ExecutionTime作为值。甚至为键和对象分配索引作为值?我使用过List,但是我想将其更改为字典,以提高搜索效率和性能。
var model = JsonConvert.DeserializeObject<Dictionary<int,ReportInfoDto>>(dtoResponse.Result);
一个附加说明,我正在使用linq查询搜索字典。那是好的做法还是有更有效的方法?
var itemsToSendOut = from reports in model
where reports.Value.ExecutionTimeFormatted == DateTime.Now.ToString("hh:mm tt ")
select reports;
这就是我调用API并获得带有对象列表的json响应的方式。
private HttpClient client;
client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:57595/");
var getallReports = client.Getasync("api/AllReports").Result;
if (getallReports.IsSuccessStatusCode)
{
var dtoResponse = getallReports.Content.ReadAsStringAsync();
dtoResponse.Wait();
var model = JsonConvert.DeserializeObject<Dictionary<int,ReportInfoDto>>(dtoResponse.Result,new keyvaluepairConverter());
var itemsToSendOut = from reports in model
where reports.Value.ExecutionTimeFormatted == DateTime.Now.ToString("hh:mm tt ")
select reports;
这是示例数据和确切格式,调试时结果给了我。
Result = "[{\"PK_Task\":3,\"TaskName\":\"ExampleReportName\",\"Businessname\":\"ExampleBusinessname\",\"UserOwner\":\"Stack Leet\",\"UsersDto\":{\"PK_User\":123,username\":\"Stack.L ...