2016-04-07 4 views
1

Я хочу вернуть результат запроса linq в формате json. Я искал часы.Возвращаемый результат Entity Framework как JSON

Вот мой код:

public IEnumerable<callersW> GetAllCallersF() 
{ 
    testCDREntities1 context = this.CurrentDataSource; 
    var query = (
    from oneCaller in CurrentDataSource.TestTables 
    select new 
    { 
     Created = oneCaller.Created, 
     Answered = oneCaller.Answered, 
     Destroyed = oneCaller.Destroyed, 
     CallerID = oneCaller.CallerId, 
     CalledID = oneCaller.CalledId, 
     DisconnectionCode = oneCaller.DisconnectionCode, 
     RTP_Caller_G107MOS = oneCaller.RTP_Caller_G107MOS, 
     RTP_Caller_LostPackets = oneCaller.RTP_Caller_LostPackets, 
     RTP_Caller_MaxRfc3550Jitter = oneCaller.RTP_Caller_MaxRfc3550Jitter, 
     RTP_Caller_MeanRfc3550Jitter = oneCaller.RTP_Caller_MeanRfc3550Jitter, 
     RTP_Called_G107MOS = oneCaller.RTP_Called_G107MOS, 
     RTP_Called_LostPackets = oneCaller.RTP_Called_LostPackets, 
     RTP_Called_MaxRfc3550Jitter = oneCaller.RTP_Called_MaxRfc3550Jitter, 
     RTP_Called_MeanRfc3550Jitter = oneCaller.RTP_Called_MeanRfc3550Jitter, 
    }).ToList() 
    .Select(x => new callersW 
    { 
     Created = Convert.ToDateTime(x.Created), 
     Answered = Convert.ToDateTime(x.Answered), 
     Destroyed = Convert.ToDateTime(x.Destroyed), 
     CallerID = x.CallerID, 
     CalledID = x.CalledID, 
     DisconnectionCode = Convert.ToInt32(x.DisconnectionCode), 
     RTP_Caller_G107MOS = Convert.ToDouble(x.RTP_Caller_G107MOS), 
     RTP_Caller_LostPackets = Convert.ToDouble(x.RTP_Caller_LostPackets), 
     RTP_Caller_MaxRfc3550Jitter = Convert.ToDouble(x.RTP_Caller_MaxRfc3550Jitter), 
     RTP_Caller_MeanRfc3550Jitter = Convert.ToDouble(x.RTP_Caller_MeanRfc3550Jitter), 
     RTP_Called_G107MOS = Convert.ToDouble(x.RTP_Called_G107MOS), 
     RTP_Called_LostPackets = Convert.ToDouble(x.RTP_Called_LostPackets), 
     RTP_Called_MaxRfc3550Jitter = Convert.ToDouble(x.RTP_Called_MaxRfc3550Jitter), 
     RTP_Called_MeanRfc3550Jitter = Convert.ToDouble(x.RTP_Called_MeanRfc3550Jitter) 
    }).ToList(); 

    return query; 
} 

Может кто-нибудь помочь мне с этим?

+0

20 секунд googling: http://stackoverflow.com/q/5701077/861716 –

ответ

2

Добавить в свой проект JSON.NET и сериализовать объект, например:

string json = JsonConvert.SerializeObject(query); 

Больше примеров here.