C# 如何从不同的表中将不同的列合并到单个列中
C# 如何从不同的表中将不同的列合并到单个列中,c#,asp.net,sql-server-2008,C#,Asp.net,Sql Server 2008,我正在尝试将各个教员的课程表合并为一个主课程表。您可以使用lambda或LINQ来完成此任务。我提供了一个有效的例子 namespace ConsoleApplication1 { using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) {
我正在尝试将各个教员的课程表合并为一个主课程表。您可以使用lambda或LINQ来完成此任务。我提供了一个有效的例子
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
List<Container> table1 = new List<Container>()
{
new Container { Column1 = "Sam", Column2 = "Fred" },
new Container { Column1 = "Arnold", Column2 = "Frank" }
};
List<Container> table2 = new List<Container>()
{
new Container { Column1 = "Whitwicky", Column2 = "Flintstone" },
new Container { Column1 = "Schwartz", Column2 = "Sinatra" }
};
var result = Example.MergeTables(table1, table2);
foreach (var r in result)
{
Console.WriteLine(r.Column1 + "\t\t" + r.Column2);
}
Console.ReadLine();
}
}
public class Example
{
public static IEnumerable<Container> MergeTables(List<Container> table1, List<Container> table2)
{
return table1.Select((value, index) =>
new Container
{
Column1 = value.Column1 + " " + table2[index].Column1,
Column2 = value.Column2 + " " + table2[index].Column2
});
}
}
public class Container
{
public string Column1 { get; set; }
public string Column2 { get; set; }
}
}
那么,您是否熟悉SQL,如果不熟悉的话,可以在google上搜索如何连接列名,也可以在这里查找如何使用连接,这是一个很好的链接,可以使用什么来确定cat是否适合风扇?这两个表是否都有一个共同的ID,因为您不能依赖未排序的顺序。看起来您需要使用Union将表组合在一起。有关如何使用Union的示例,请查看下面的链接。我还建议您使用JOIN,但我不确定这两行中是否有公共列,或者是否有第三个表与其他两个表具有公共行。LINQ或lambda只是一种查询语言。你几乎可以在任何数据收集中使用它。我对asp非常陌生,所以你可以用表示例来说明我建议你研究ADO.NET EntityFramework,这里有一个链接可以帮助你入门。