我有以下代码:
public class Post
{
public int countSubject;
public string dayName;
public string[] subjectName;
public string[] auditory;
public string[] subjectType;
public string[] TimeStart;
public string[] TimeEnd;
public string[] Teacher;
public Post(int countSubject, string dayName, string[] subjectName, string[] auditory, string[] subjectType, string[] TimeStart,
string[] TimeEnd, string[] Teacher)
{
this.dayName = dayName;
this.countSubject = countSubject;
this.subjectName = subjectName;
this.auditory = auditory;
this.subjectType = subjectType;
this.TimeStart = TimeStart;
this.TimeEnd = TimeEnd;
this.Teacher = Teacher;
}
}
List<Post> posts = new List<Post>();
posts.Add(new Post(5, "Monday", new string[] { "Свободная", "Физика", "ИНТЕХ", "ИНФОРМАТИКА" },
new string[] { "", "320", "3201", "108" }, new string[] { "", "Лекция", "Практика", "Лабораторная" },
new string[] { "08:05", "09:50", "11:35", "13:35" }, new string[] { "9:35", "11:20", "13:05", "15:05" },
new string[] { "", "Повх Л.А", "Ямполь Е.С", "Лихозвон" }));
posts.Add(new Post(5, "Tuesday", new string[] { "УПИФ", "ИНФОРМАТИКА", "МАТАН", "МАТАН" },
new string[] { "", "320", "3201", "108" }, new string[] { "Лекция", "Практика", "Лекция", "Практика" },
new string[] { "08:05", "09:50", "11:35", "13:35" }, new string[] { "9:35", "11:20", "13:05", "15:05" },
new string[] { "Лолка А.А", "Повх Л.А", "Ямполь Е.С", "Лихозвон" }));
JObject rss = new JObject(
new JProperty("Timetable",
new JArray(
new JObject(
new JProperty("CountSubject", "5"),
from q in posts
select
new JProperty(q.dayName,
from d in q.subjectType
select
new JObject(
new JProperty("SubjectName", ""),
new JProperty("Auditory", ""),
new JProperty("SubjectType", d),
new JProperty("TimeStart", ""),
new JProperty("TimeEnd", ""),
new JProperty("Teacher", "")))))));
在控制台中获取此输出:
{
"Timetable": [
{
"CountSubject": "5",
"Monday": [
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
},
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Лекция",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
},
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Практика",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
},
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Лабораторная",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
}
],
"Tuesday": [
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Лекция",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
},
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Практика",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
},
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Лекция",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
},
{
"SubjectName": "",
"Auditory": "",
"SubjectType": "Практика",
"TimeStart": "",
"TimeEnd": "",
"Teacher": ""
}
]
}
]
}
问题是,主题类型填写正确,但字段的其余部分无法填写,因为在q.subjectType中的"from d"之后添加第二个带有q.subject Name等的"from"会导致一个重复循环,并获得重复的JSON对象。
必须是:
{
"Timetable": [
{
"CountSubject": "5",
"Monday": [
{
"SubjectName": "Свободная",
"Auditory": "",
"SubjectType": "",
"TimeStart": "08:05",
"TimeEnd": "09:35",
"Teacher": ""
},
{
"SubjectName": "Физика",
"Auditory": "320",
"SubjectType": "Лекция",
"TimeStart": "09:50",
"TimeEnd": "11:20",
"Teacher": "Повх Л.А"
},
{
"SubjectName": "ИНТЕХ",
"Auditory": "3201",
"SubjectType": "Практика",
"TimeStart": "11:35",
"TimeEnd": "13:05",
"Teacher": "Ямполь Е.С"
},
{
"SubjectName": "ИНФОРМАТИКА",
"Auditory": "108",
"SubjectType": "Лабораторная",
"TimeStart": "13:35",
"TimeEnd": "15:05",
"Teacher": "Лихозвон"
}
],
"Tuesday": [
{
"SubjectName": "УПИФ",
"Auditory": "320",
"SubjectType": "Лекция",
"TimeStart": "08:05",
"TimeEnd": "09:35",
"Teacher": "Лолка А.А"
},
{
"SubjectName": "ИНФОРМАТИКА",
"Auditory": "",
"SubjectType": "Практика",
"TimeStart": "09:50",
"TimeEnd": "11:20",
"Teacher": "Повх Л.А"
},
{
"SubjectName": "МАТАН",
"Auditory": "",
"SubjectType": "Лекция",
"TimeStart": "11:35",
"TimeEnd": "13:05",
"Teacher": "Ямполь Е.С"
},
{
"SubjectName": "МАТАН",
"Auditory": "",
"SubjectType": "Практика",
"TimeStart": "13:35",
"TimeEnd": "15:05",
"Teacher": "Лихозвон"
}
]
}
]
}
对不起,我的英语不好,谷歌翻译
当您使用两个部分"from"时,您将收到笛卡尔乘法,而不是连接两个集合。要得到你想要的,试着这样重写:
from index in Enumerable.Range(0, q.subjectType.Length)
select
new JObject(
new JProperty("SubjectName", q.subjectName[index]),
new JProperty("Auditory", q.auditory[index]),
new JProperty("SubjectType", q.subjectType[index]),
new JProperty("TimeStart", q.TimeStart[index]),
new JProperty("TimeEnd", q.TimeEnd[index]),
new JProperty("Teacher", q.Teacher[index])))))));