Lambda expressions and Query expressions (2) Advanced use

Source: Internet
Author: User
Introduction · first-return the first element in the Set; no delay · firstordefault-return the first element in the Set (if not, return the default value ); no Delay · last-return the last element in the Set; no delay · lastordefault-return the last element in the Set (if not, return the default value) · elementat-return the element of the specified index in the Set; no delay · elementatordefault-return the element of the specified index in the Set (if not, the default value is returned ); no Delay · contains-determines whether a collection contains a certain element; no delay · any-determines whether an element meets a certain condition in the collection; no Delay · All-determine whether all elements in the Set meet a certain condition; no delay · count-return the number of elements in the set and return int; no Delay · longcount-return the number of elements in the set and return long; no delay · sum-the set should be a set of numerical types to calculate their sum; no delay · min-return the minimum value of the Set; no Delay · max-return the maximum value of the Set; no delay · average-the set should be a set of numerical types, and calculate its average value; no delay · aggregate-obtain an aggregate value based on the input expression; no Delay · Cast-convert the set to a strongly typed set; delay · defaultifempty-if the query result is null, the default value is returned; delay · sequenceequal-determines whether the two sets are the same; no Delay-oftype-filter the specified type in the Set; delay-toarray-convert the set to an array; no delay-tolist-convert the set to a list <t> set; no Delay-todictionary-converts a set to a <K, V> set without delay

 

1. selectselect projects in a set sequence according to the given conditions. Select can return the filtering results of the combination, return the anonymous type, operate on the returned results, and return the combined subquery results. Select method definition prototype: public static ienumerable <tresult> select <tsource, tresult> (this ienumerable <tsource> source, func <tsource, tresult> selector) this extension method is defined in the enumerable type: // The property var result = from student in datasource of the data source type. students where student. name. length> 3 select student. name; // the result of data source type filtering var result = from student in datasource. students where student. name. length> 3 select student; // var result of the New Type = From student in datasource. students where student. name. length> 3 select new student {name = student. name, studentid = student. studentid}; // anonymous var result = from student in datasource. students where student. name. length> 3 select new {name = student. name, studentid = student. studentid}; // perform var result = from student in datasource on the returned results. students where student. name. length> 3 select student. Tostring (); As can be seen from the prototype of the select method, the returned result is ienumerable <t> type. 2. selectmanyselectult: public static ienumerable <tresult> selectult <tsource, tresult> (this ienumerable <tsource> source, func <tsource, ienumerable <tresult> selector) from the prototype, we can see that each element type of the filtering result should implement the ienumerable <t> interface: String implements the ienumerable <t> interface, and can construct such a scenario: query the sequences of all characters that comprise the Student name. VaR result = datasource. students. selectiterator (STR => Str. name); equivalent select clause: var result = from student in datasource. students from CH in student. name select ch; it can be considered that selectsequence projects each element of the sequence to ienumerable <(of <(T>)> and merges the result sequence into a sequence. 3. The prototype of distinct is public static ienumerable <tsource> distinct <tsource> (this ienumerable <tsource> source) to remove duplicate elements from the projection result set. This operation can only be performed by calling methods. In the same scenario: query the non-Repeated sequences of all characters that constitute the Student name. VaR result = datasource. Students. selectct (STR => Str. Name). Distinct (); 4. first, last, Skip, take, singlefirst select the first element of the set. VaR result = datasource. Students. Select (student => Student). First (); // student ID: 1, Student name: andylast select the last element of the set. VaR result = datasource. Students. Select (student => Student). Last (); // student ID: 4, Student name: darkskip skips n elements. VaR result = datasource. Students. Select (student => Student). Skip (2). Count (); // 2 take selects the first n elements of the set. VaR result = datasource. students. select (student => Student ). skip (2 ). take (1 ). count (); // 1 single returns the unique element of the sequence. If the sequence does not exactly contain an element, an exception is thrown. VaR result = datasource. Students. Select (student => Student). Single (); // exception: sequence contains more than one element5. orderby [Descending] sorts the set. Public static iorderedenumerable <tsource> orderby [Descending] <tsource, tkey> (this ienumerable <tsource> source, func <tsource, tkey> keyselector [, icomparer <tkey> comparer]) tkey must have implemented the icomparer <t> interface.
Method 1: var result = from student in datasource. students orderby student. name descending select student. name; Method 2: var result = datasource. students. orderbydescending (student => student. name ). select (student => student. name );
Result: // dark // Cindy // Bill // Andy

1. Application of reflection

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. Transform Application in Unity

Bool retrievefightzones () {var nodetransform = stageinstance. transform. findchildbypath (battleconstants. stageconstants. fightzonesnodepath); foreach (transform fightzonetransform in nodetransform) {fightzonetransform. cast <transform> () // convert to the transform type. select (x => X. gameobject) // X indicates the transformed transform and then obtains its gameobject. tolist () // convert gameobject to a list <t> set. foreach (gameobject. destroy); // destroy gameobject cyclically }}
Void updatepositions ()
{// Convert to transform select position and convert to array output positions = transform. Cast <transform> (). Select (t => T. Position). toarray ();}
Void setuppositions () {// convert to transform to convert the current position to an array and return positions = transform. cast <transform> (). select (x => {var newpos = x. position; return newpos ;}). toarray ();}

 

3. Use ienumerator in Unity together

Private ienumerator initializebattleasync () {var charactersloading = battlecharactermanager. createcharacters (
// Parameter 1 converts the first three returned result links of the array charactercreationinfo and debugaddmorecharacters functions to the array new charactercreationinfo [] {New charactercreationinfo (charactertype. me )}. concat (debugaddmorecharacters (). take (3 )). toarray (),
// Parameter 2 Charas =>{ allies = Charas; battlesummonmanager = new battlesummonmanager (summon); battlesummonmanager. setallies (Charas); battlefightmanager. beginbattle (battlesummonmanager, new characterselector (metrics, enemies) ;}); yield return parameters;} private ienumerable <charactercreationinfo> encrypt () {yield return New charactercreationinfo. help Er, 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]);}

 

Lambda expressions and Query expressions (2) Advanced use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.