1.Enumerable.Concat<(Of <(TSource>)>) 泛型方法应用
此方法是延迟执行的。此方法只返回输入序列中的所有原始元素
应用场景:只有通过直接调用对象的 GetEnumerator 方法来枚举该对象时,才执行此方法表示的查询。
IEnumerable<String> queryWithEnumerable = students.Select(student => student.City).Concat(teachers.Select(teacher => teacher.City));
Console.WriteLine("Enumerable.Concat<(Of <(TSource>)>) 泛型方法应用");
foreach (string city in queryWithEnumerable)
Console.WriteLine(city);
Console.WriteLine("Enumerable.Concat<(Of <(TSource>)>) 泛型方法应用");
foreach (string city in queryWithEnumerable)
Console.WriteLine(city);
2.QueryableConcat<(Of <(TSource>)>) 泛型方法应用
此方法生成 MethodCallExpression,其实方法本身即作为了构造的泛型方法。
然后,将 MethodCallExpression 传递给 IQueryProvider 的 CreateQuery方法,由 source1 参数的 Provider 属性表示。其实说白了,就是将 source2 中的元素连接到 source1 中的元素以组合成一个新的序列。
IEnumerable<String> queryWithQueryable = students.AsQueryable().Select(student => student.City).Concat(teachers.Select(teacher => teacher.City));
Console.WriteLine("QueryableConcat<(Of <(TSource>)>) 泛型方法应用");
foreach (string city in queryWithEnumerable)
Console.WriteLine(city);
Console.WriteLine("QueryableConcat<(Of <(TSource>)>) 泛型方法应用");
foreach (string city in queryWithEnumerable)
Console.WriteLine(city);
以下是一个完整的DEMO
public class ChangeDataUsingLinq : Interface
{
Interface Members
class Student
{
public string First { get; set; }
public string Last { get; set; }
public int ID { get; set; }
public string Street { get; set; }
public string City { get; set; }
public List<int> Scores;
}
class Teacher
{
public string First { get; set; }
public string Last { get; set; }
public int ID { get; set; }
public string City { get; set; }
}
}
{
Interface Members
class Student
{
public string First { get; set; }
public string Last { get; set; }
public int ID { get; set; }
public string Street { get; set; }
public string City { get; set; }
public List<int> Scores;
}
class Teacher
{
public string First { get; set; }
public string Last { get; set; }
public int ID { get; set; }
public string City { get; set; }
}
}