C#,MVC,EnityFramework,如何处理数据

时间:2016-10-21 07:33:54

标签: c# json asp.net-mvc string

我有这个数据

data table

我想将Day转换为英文值 例如 : “السبت”=“星期六” “الاحد”=“星期日”

此代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr>
  <td>
    <input type="checkbox" id="chk">
    <input type="text" id="txt">
  </td>
</tr>
<input type="text" id="txt2">
<input type="button" id="btn" value="Click">

返回:

 [HttpGet]
    public JsonResult GetTime()
    {
        var data = _db.CourseTimes
     .GroupBy(c => c.Day)
     .ToDictionary(g => g.Key, v => v.Select(c => new { start = TimeSpan.FromHours(c.StartTime)
     .ToString("hh':'mm"), stop = TimeSpan.FromHours(c.EndTime).ToString("hh':'mm") }));

        return Json(data, JsonRequestBehavior.AllowGet);
    }

我想让它归还:

result

2 个答案:

答案 0 :(得分:2)

好的,这是一个如何实现的实现:

[HttpGet]
public JsonResult GetTime()
{
    var english_names = (new CultureInfo("en-US")).DateTimeFormat.DayNames
        .Select((v, i) => new { Key = i, Value = v })
        .ToDictionary(o => o.Key, o => o.Value);

    var arabic_names = (new CultureInfo("ar-EG")).DateTimeFormat.DayNames
        .Select((v, i) => new { Key = i, Value = v })
        .ToDictionary(o => o.Key, o => o.Value);

    var data = _db.CourseTimes
        .GroupBy(c => c.Day)
        .ToDictionary(g => english_names.FirstOrDefault(p => p.Key == arabic_names.FirstOrDefault(a => a.Value == g.Key).Key).Value, v => v.Select(c => new
        {
            start = TimeSpan.FromHours(c.StartTime).ToString("hh':'mm"),
            stop = TimeSpan.FromHours(c.EndTime).ToString("hh':'mm")
        }).ToList());

    return Json(data, JsonRequestBehavior.AllowGet);
}

基本上,你只需要为每种语言创建两个名称列表,并获得阿拉伯语名称的英语索引,该名称可以在第二个列表中找到。

答案 1 :(得分:0)

尝试使用CultureInfo,或者您必须将单词映射到表格或静态列表,并比较该单词...