将 3 个列表组合成 1 个 LINQ 查询 C#
Combining 3 list into 1 LINQ Query C#
我是编程新手,我想知道是否可以将索引上的 3 个不同 table 连接到一个查询结果中。或者,如果无法使用 LINQ 加入,我可以创建一个包含 3 个列表数据的新列表。
(Ps。列表的内容将由用户输入,项目的数量可以从 运行 运行 变化,但列表中的项目数量将永远一样)
这是 3 个列表:
List<string> SelfStudyModuleName = new List<string>();
public List<double> SelfStudySesionHours = new List<double>();
public List<DateTime> SelfStudySesiondate = new List<DateTime>();
各榜单样本数据:
SelfStudyModuleName.Add("Prog");
SelfStudyModuleName.Add("Cloud");
SelfStudyModuleName.Add("Database");
SelfStudyModuleName.Add("Geo");
SelfStudySesionHours.Add(2);
SelfStudySesionHours.Add(5);
SelfStudySesionHours.Add(3);
SelfStudySesionHours.Add(6);
SelfStudySesiondate[0]=2021/12/01;
SelfStudySesiondate[1]=2021/12/02;
SelfStudySesiondate[2]=2021/12/03;
SelfStudySesiondate[3]=2021/12/04;
所需结果的样本出现:
2021/12/01 Prog 2
2021/12/02 Cloud 5
2021/12/03 DataBase 3
2021/12/04 Geo 6
试试这个
var result = new List<SelfStudy>();
for (var i = 0; i < SelfStudyModuleName.Count; i++)
{
result.Add(new SelfStudy
{
SelfStudyModuleName = SelfStudyModuleName[i],
SelfStudySesionHours = SelfStudySesionHours[i],
SelfStudySesiondate = SelfStudySesiondate[i]
});
}
}
class
public class SelfStudy
{
public DateTime SelfStudySesiondate { get; set; }
public string SelfStudyModuleName { get; set; }
public double SelfStudySesionHours { get; set; }
}
并为您的初始化添加 SelfStudySesiondate0
public string[] SelfStudySesiondate0 = new string[4];
SelfStudySesiondate0[0] = "2021 / 12 / 01";
SelfStudySesiondate0[1] = "2021 / 12 / 02";
SelfStudySesiondate0[2] = "2021 / 12 / 03";
SelfStudySesiondate0[3] = "2021 / 12 / 04";
SelfStudySesiondate=SelfStudySesiondate0.Select(i=> Convert.ToDateTime( i)).ToList();
元组 也可能有帮助。
如果您想避免使用 类,只需使用这个即可。
var result = new tuple();
for (var i = 0; i < SelfStudyModuleName.Count; i++)
{
result.Add(new SelfStudy
{
SelfStudyModuleName[i],SelfStudySesionHours[i],SelfStudySesiondate[i]
});
}
}
如果你不想再做一个class你可以这样做。否则请参阅@Serge 的回答
var SelfStudyModuleName = new List<string>();
var SelfStudySesionHours = new List<double>();
var SelfStudySesiondate = new List<DateTime>();
SelfStudyModuleName.Add("Prog");
SelfStudyModuleName.Add("Cloud");
SelfStudyModuleName.Add("Database");
SelfStudyModuleName.Add("Geo");
SelfStudySesionHours.Add(2);
SelfStudySesionHours.Add(5);
SelfStudySesionHours.Add(3);
SelfStudySesionHours.Add(6);
SelfStudySesiondate[0] = new DateTime(2021, 12, 1);
SelfStudySesiondate[1] = new DateTime(2021, 12, 2);
SelfStudySesiondate[2] = new DateTime(2021, 12, 3);
SelfStudySesiondate[3] = new DateTime(2021, 12, 4);
var combined = new List<(string Name, double Hours, DateTime Date)>();
for (int i = 0; i < SelfStudyModuleName.Count; i++)
{
combined[i] = ( Name: SelfStudyModuleName[i], Hours: SelfStudySesionHours[i], Date: SelfStudySesiondate[i] );
}
LINQ Zip
运算符可能就是您要查找的内容:
var results = SelfStudyModuleName.Zip(SelfStudySesionHours, (moduleName, sessionHours) => new { moduleName, sessionHours })
.Zip(SelfStudySesiondate, (e, date) => new { e.moduleName, e.sessionHours, date});
虽然如果你有能力索引到源(就像你可以使用 List/Array),你可以做这样的事情:
var results2 = SelfStudyModuleName
.Select((moduleName, i) => new {
moduleName,
sessionHours = SelfStudySesionHours[i],
date = SelfStudySesiondate[i]
});
我是编程新手,我想知道是否可以将索引上的 3 个不同 table 连接到一个查询结果中。或者,如果无法使用 LINQ 加入,我可以创建一个包含 3 个列表数据的新列表。
(Ps。列表的内容将由用户输入,项目的数量可以从 运行 运行 变化,但列表中的项目数量将永远一样)
这是 3 个列表:
List<string> SelfStudyModuleName = new List<string>();
public List<double> SelfStudySesionHours = new List<double>();
public List<DateTime> SelfStudySesiondate = new List<DateTime>();
各榜单样本数据:
SelfStudyModuleName.Add("Prog");
SelfStudyModuleName.Add("Cloud");
SelfStudyModuleName.Add("Database");
SelfStudyModuleName.Add("Geo");
SelfStudySesionHours.Add(2);
SelfStudySesionHours.Add(5);
SelfStudySesionHours.Add(3);
SelfStudySesionHours.Add(6);
SelfStudySesiondate[0]=2021/12/01;
SelfStudySesiondate[1]=2021/12/02;
SelfStudySesiondate[2]=2021/12/03;
SelfStudySesiondate[3]=2021/12/04;
所需结果的样本出现:
2021/12/01 Prog 2
2021/12/02 Cloud 5
2021/12/03 DataBase 3
2021/12/04 Geo 6
试试这个
var result = new List<SelfStudy>();
for (var i = 0; i < SelfStudyModuleName.Count; i++)
{
result.Add(new SelfStudy
{
SelfStudyModuleName = SelfStudyModuleName[i],
SelfStudySesionHours = SelfStudySesionHours[i],
SelfStudySesiondate = SelfStudySesiondate[i]
});
}
}
class
public class SelfStudy
{
public DateTime SelfStudySesiondate { get; set; }
public string SelfStudyModuleName { get; set; }
public double SelfStudySesionHours { get; set; }
}
并为您的初始化添加 SelfStudySesiondate0
public string[] SelfStudySesiondate0 = new string[4];
SelfStudySesiondate0[0] = "2021 / 12 / 01";
SelfStudySesiondate0[1] = "2021 / 12 / 02";
SelfStudySesiondate0[2] = "2021 / 12 / 03";
SelfStudySesiondate0[3] = "2021 / 12 / 04";
SelfStudySesiondate=SelfStudySesiondate0.Select(i=> Convert.ToDateTime( i)).ToList();
元组
for (var i = 0; i < SelfStudyModuleName.Count; i++)
{
result.Add(new SelfStudy
{
SelfStudyModuleName[i],SelfStudySesionHours[i],SelfStudySesiondate[i]
});
}
}
如果你不想再做一个class你可以这样做。否则请参阅@Serge 的回答
var SelfStudyModuleName = new List<string>();
var SelfStudySesionHours = new List<double>();
var SelfStudySesiondate = new List<DateTime>();
SelfStudyModuleName.Add("Prog");
SelfStudyModuleName.Add("Cloud");
SelfStudyModuleName.Add("Database");
SelfStudyModuleName.Add("Geo");
SelfStudySesionHours.Add(2);
SelfStudySesionHours.Add(5);
SelfStudySesionHours.Add(3);
SelfStudySesionHours.Add(6);
SelfStudySesiondate[0] = new DateTime(2021, 12, 1);
SelfStudySesiondate[1] = new DateTime(2021, 12, 2);
SelfStudySesiondate[2] = new DateTime(2021, 12, 3);
SelfStudySesiondate[3] = new DateTime(2021, 12, 4);
var combined = new List<(string Name, double Hours, DateTime Date)>();
for (int i = 0; i < SelfStudyModuleName.Count; i++)
{
combined[i] = ( Name: SelfStudyModuleName[i], Hours: SelfStudySesionHours[i], Date: SelfStudySesiondate[i] );
}
LINQ Zip
运算符可能就是您要查找的内容:
var results = SelfStudyModuleName.Zip(SelfStudySesionHours, (moduleName, sessionHours) => new { moduleName, sessionHours })
.Zip(SelfStudySesiondate, (e, date) => new { e.moduleName, e.sessionHours, date});
虽然如果你有能力索引到源(就像你可以使用 List/Array),你可以做这样的事情:
var results2 = SelfStudyModuleName
.Select((moduleName, i) => new {
moduleName,
sessionHours = SelfStudySesionHours[i],
date = SelfStudySesiondate[i]
});