Lambda表达式和查询表达式(2)高级使用

时间:2022-08-20 18:53:20
介绍
    ·First - 返回集合中的第一个元素;不延迟
    ·FirstOrDefault - 返回集合中的第一个元素(如果没有则返回默认值);不延迟
    ·Last - 返回集合中的最后一个元素;不延迟
    ·LastOrDefault - 返回集合中的最后一个元素(如果没有则返回默认值)
    ·ElementAt - 返回集合中指定索引的元素;不延迟
    ·ElementAtOrDefault - 返回集合中指定索引的元素(如果没有则返回默认值);不延迟
    ·Contains - 判断集合中是否包含有某一元素;不延迟
    ·Any - 判断集合中是否有元素满足某一条件;不延迟
    ·All - 判断集合中是否所有元素都满足某一条件;不延迟
    ·Count - 返回集合中的元素个数,返回int;不延迟
    ·LongCount - 返回集合中的元素个数,返回long;不延迟
    ·Sum - 集合应为数字类型集合,求其和;不延迟
    ·Min - 返回集合的最小值;不延迟
    ·Max - 返回集合的最大值;不延迟
    ·Average - 集合应为数字类型集合,求其平均值;不延迟
    ·Aggregate - 根据输入的表达式获取一个聚合值;不延迟
    ·Cast - 将集合转换为强类型集合;延迟
    ·DefaultIfEmpty - 查询结果为空则返回默认值;延迟
    ·SequenceEqual - 判断两个集合是否相同;不延迟
    ·OfType - 过滤集合中的指定类型;延迟
    ·ToArray - 将集合转换为数组;不延迟
    ·ToList - 将集合转换为List<T>集合;不延迟
    ·ToDictionary - 将集合转换为<K, V>集合;不延迟

 

1. Select
select 在一个集合序列按给定的条件进行投影,select 可以返回组合的筛选结果,返回匿名类型,对返回结果进行操作,返回组合的子查询结果等等。
select 的方法定义原形为:
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)

该扩展方法是在Enumerable类型中定义的:
// 数据源的类型的属性
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select student.Name;

// 数据源类型筛选后的结果
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select student;

// 新类型
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select new Student { Name = student.Name, StudentID = student.StudentID };

// 匿名类型
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select new { Name = student.Name, StudentID = student.StudentID };

// 对返回结果进行操作
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select student.ToString();

由Select方法原型可看出,返回结果为:IEnumerable<T>类型。

2. SelectMany
SelectMany定义原型为:
public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)

通过原型可以看出,筛选结果的每一个元素类型都应该实现IEnumerable<T>接口:
string实现了IEnumerable<T>接口,可以构造这样的场景:查询组成学生姓名的所有字符序列。
var result = DataSource.Students.SelectMany(str => str.Name);

等价的Select 子句为:
var result = from student in DataSource.Students
             from ch in student.Name
             select ch;

可以认为SelectMany是将序列的每个元素投影到 IEnumerable<(Of <(T>)>) 并将结果序列合并为一个序列。

3.Distinct
原型为:
public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source)
去掉投影结果集的重复元素。该运算只能以方法调用的方式进行操作。
上面同样的场景:查询组成学生姓名的所有字符的不重复序列。
var result = DataSource.Students.SelectMany(str => str.Name).Distinct();

4. First、Last、Skip、Take、Single
First 选择集和的第一个元素。
var result = DataSource.Students.Select(student => student).First(); // Student ID:1,Student Name:Andy
Last 选择集和的最后一个元素。
var result = DataSource.Students.Select(student => student).Last(); // Student ID:4,Student Name:Dark
Skip 跳过N个元素。
var result = DataSource.Students.Select(student => student).Skip(2).Count(); // 2
Take 选择集合的前N个元素。
var result = DataSource.Students.Select(student => student).Skip(2).Take(1).Count(); // 1
Single 返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。
var result = DataSource.Students.Select(student => student).Single(); // 异常:Sequence contains more than one element

5. OrderBy[Descending]
对集合进行排序。
public static IOrderedEnumerable<TSource> OrderBy[Descending]<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector[, IComparer<TKey> comparer])
TKey必须已经实现了IComparer<T>接口。
方法1: var result = from student in DataSource.Students orderby student.Name descending select student.Name; 方法2: var result = DataSource.Students.OrderByDescending(student => student.Name).Select(student => student.Name);
结果: // Dark // Cindy // Bill // Andy

1.  反射的应用

public static class GameConstants
{
    public static readonly string SCENE_STARTER = "_Starter";
    public static readonly string SCENE_MOVIETITLE = "MovieTitle";
    public static readonly string SCENE_BATTLE_UI = "Battle_UI";
    public static readonly string SCENE_BATTLE_RESULT_UI = "Battle_Result_UI";
    public static readonly string[] SCENES;
    // -----------------------------------------------------------
    public static readonly string CHARACTER_IDLE_ANIMATION_NAME = "Idle";
    public static readonly string CHARACTER_ATTACK_ANIMATION_NAME = "Attack";
    public static readonly string CHARACTER_DEATH_ANIMATION_NAME = "Dead";
    public static readonly string[] CHARACTER_ACTIONS;
    // -----------------------------------------------------------
    public static readonly string TAG_PLAYER_CHARACTER = "PlayerCharacter";
    public static readonly string TAG_ENEMY_CHARACTER  = "EnemyCharacter";
    public static readonly string[] TAGS;
    // ==================================
    static GameConstants()
    {
        SCENES = AggregatePublicStaticFields("SCENE_");
        CHARACTER_ACTIONS = AggregatePublicStaticFields("CHARACTER_ACTIONS_");
        TAGS = AggregatePublicStaticFields("TAG_");
    }
    private static string[] AggregatePublicStaticFields(string prefix)
    {
        var fields = typeof(GameConstants).GetFields(BindingFlags.Public | BindingFlags.Static);
        return fields.Where(x => x.Name.StartsWith(prefix)).Select(x => (string)x.GetValue(null)).ToArray();
    }
}

 

2. Unity中Transform的应用

  bool RetrieveFightZones()
  {
        var nodeTransform = stageInstance.transform.FindChildByPath(BattleConstants.StageConstants.FightZonesNodePath);
        foreach (Transform fightZoneTransform in nodeTransform)
        {
             fightZoneTransform
                .Cast<Transform>()              // 转化为Transform类型
                .Select(x => x.gameObject)      // x代表转化后的transform 然后取它的Gameobject
                .ToList()                       // 将GameObject转换为List<T>集合
                .ForEach(GameObject.Destroy);   // 循环销毁gameObject
        }
   }
void UpdatePositions()
{ // 转化为Transform选择位置 然后转化为数组 输出 Positions = transform. Cast<Transform>() .Select(t => t.position) .ToArray(); }
void SetupPositions()
{
      // 转化为Transform 将当前位置 转化为数组返回
      Positions = transform.Cast<Transform>().Select(x =>{var newPos = x.position; return newPos; }).ToArray();
}

 

3. Unity中IEnumerator一起应用

private IEnumerator InitializeBattleAsync()
{
    var charactersLoading = battleCharacterManager.CreateCharacters(
//参数1 将数组创建的数组CharacterCreationInfo 和 DebugAddMoreCharacters函数的前三个返回的结果链接 然后转化成数组 new CharacterCreationInfo[]{new CharacterCreationInfo(CharacterType.Me)}.Concat(DebugAddMoreCharacters().Take(3)).ToArray() ,
// 参数2 charas => { allies = charas; battleSummonManager = new BattleSummonManager(summon); battleSummonManager.SetAllies(charas); battleFightManager.BeginBattle(battleSummonManager, new CharacterSelector(cameraFollowManager, enemies)); } ); yield return charactersLoading; } private IEnumerable<CharacterCreationInfo> DebugAddMoreCharacters() { yield return new CharacterCreationInfo(CharacterType.Helper, CharacterClass.Warrior); yield return new CharacterCreationInfo(CharacterType.Beast, CharacterClass.Warrior); yield return new CharacterCreationInfo(CharacterType.Beast, CharacterClass.Warrior); yield return new CharacterCreationInfo(CharacterType.Beast, CharacterClass.Warrior); yield break; var availableCharacterClasses = Enum.GetValues(typeof(CharacterClass)) .Cast<CharacterClass>() .Take(4) // filter out the player characters .Where(x => x != SceneManager.Instance.MyCharacterClass) .ToArray(); yield return new CharacterCreationInfo(CharacterType.Helper, availableCharacterClasses[0]); }

 

相关文章