Aggregate
遍历序列中所有数据,可附加初始值,可指定返回类型
using System.Linq;
using UnityEngine;
/// <summary>
/// 遍历表中所有数据,可附加初始值,可指定返回类型
/// https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.queryable.aggregate?view=netframework-4.6.1
/// </summary>
public class Aggregate : MonoBehaviour
{
private void Start()
{
string[] fruits = new string[] { "apple", "banana" };
var longest = fruits.AsQueryable().Aggregate(fruits[0], (cur, next) => next.Length > cur.Length ? next : cur);
Debug.Log(longest);//输出结果 banana
//不使用初始值
longest = fruits.AsQueryable().Aggregate((cur, next) => next.Length > cur.Length ? next : cur);
Debug.Log(longest);//输出结果 banana
//指定返回类型
longest = fruits.AsQueryable().Aggregate(fruits[0], (cur, next) => next.Length > cur.Length ? next : cur, result => result.ToUpper());
Debug.Log(longest);//输出结果 BANANA
}
}
All
确认序列中所有元素是否都满足条件,只有全部都满足才返回true。
using System.Linq;
using UnityEngine;
/// <summary>
/// 数据中所有元素满足条件
/// https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.queryable.all?view=netframework-4.6.1
/// </summary>
public class All : MonoBehaviour
{
class Pet
{
public string Name;
public int Age;
}
// Use this for initialization
void Start()
{
Pet[] pets = {
new Pet{ Name = "a", Age = 1},
new Pet{ Name = "b", Age = 2},
new Pet{ Name = "c", Age = 3},
};
var query = pets.AsQueryable();
//判断所有宠物是否都大于0岁
var isTrue = query.All(pet => pet.Age > 0);
Debug.Log(isTrue);//输出结果:true
}
}
Any
确认序列中是否有任意元素满足条件返回true,否则false
using System.Linq;
using UnityEngine;
/// <summary>
/// 数据中任何元素满足条件
/// https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.queryable.any?view=netframework-4.6.1
/// </summary>
public class Any : MonoBehaviour
{
class Pet
{
public string Name;
public int Age;
}
// Use this for initialization
void Start()
{
Pet[] pets = {
new Pet{ Name = "a", Age = 1},
new Pet{ Name = "b", Age = 2},
new Pet{ Name = "c", Age = 3},
};
//序列是否包含任意元素
Debug.Log(pets.Any());//false
//是否有狗狗年龄超过3岁
Debug.Log(pets.Any(pet => pet.Age > 3));//false
}
}
Average
计算序列元素的平均值
using System.Linq;
using UnityEngine;
/// <summary>
/// 计算平均值,可计算包含null的可空值类型
/// https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.queryable.average?view=netframework-4.6.1
/// </summary>
public class Average : MonoBehaviour
{
class Pet
{
public string Name;
public int Age;
}
// Use this for initialization
void Start()
{
Pet[] pets = {
new Pet{ Name = "a", Age = 1},
new Pet{ Name = "b", Age = 2},
new Pet{ Name = "c", Age = 3},
};
//计算狗狗的平均年龄
Debug.Log(pets.Average(pet => pet.Age));//2
//计算包含null数组的平均值
int?[] numbers = { 1, 2, null, 4, 3 };
double? average = numbers.Average();
Debug.Log(average);//2.5
}
}
Cast
将序列元素转换为指定类型
using System.Collections;
using System.Linq;
using UnityEngine;
/// <summary>
/// 将 IQueryable 的元素转换为指定的类型。
/// https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.queryable.cast?view=netframework-4.6.1
/// </summary>
public class Cast : MonoBehaviour
{
class Pet
{
public string Name;
public int Age;
public override string ToString()
{
return string.Format("name: {0}, age: {1}", Name, Age);
}
}
// Use this for initialization
void Start()
{
ArrayList words = new ArrayList { "a", "b", "c" };
var strs = words.Cast<string>();
foreach (var str in strs)
Debug.Log(str);
//结果:"a"、"b"、"c"
}
}
Concat
连接两个序列
using System.Linq;
using UnityEngine;
/// <summary>
/// 连接两个序列。
/// https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.queryable.concat?view=netframework-4.6.1
/// </summary>
public class Concat : MonoBehaviour
{
// Use this for initialization
void Start()
{
int[] a = { 1, 2, 3 };
int[] b = { 4, 5, 6 };
_ = a.Concat(b);//{1,2,3,4,5,6}
}
}
Contains
确定序列是否包含指定的元素。可指定IEqualityComparer。
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
/// <summary>
/// 通过使用指定的 IEqualityComparer<T> 确定序列是否包含指定的元素。
/// https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.queryable.contains?view=netframework-4.6.1
/// </summary>
public class Contains : MonoBehaviour
{
class Pet : IEqualityComparer<Pet>
{
public string Name;
public int Age;
public bool Equals(Pet x, Pet y)
{
Debug.Log("asdfsdaf");
return x.Name == y.Name && x.Age == y.Age;
}
public int GetHashCode(Pet obj)
{
return Name.GetHashCode();
}
}
// Use this for initialization
void Start()
{
Pet[] pets = {
new Pet{ Name = "a", Age = 1},
new Pet{ Name = "b", Age = 2},
new Pet{ Name = "c", Age = 3},
};
var pet = new Pet { Name = "a", Age = 1 };
var comparer = pet;
//指定Comparer
Debug.Log(pets.Contains(pet, comparer));//true
//使用默认的comparer
Debug.Log(pets.Contains(pet));//false 不是同一个引用
}
}
Count
返回序列中满足条件的元素数量, 当没有条件时,返回序列长度
using System.Linq;
using UnityEngine;
/// <summary>
/// 返回序列中的元素数量。
/// Count<TSource>(IQueryable<TSource>)
/// 返回指定序列中满足条件的元素数量。
/// Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
/// https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.queryable.count?view=netframework-4.6.1
/// </summary>
public class Count : MonoBehaviour
{
// Use this for initialization