Let Lua support LINQ.

Source: Internet
Author: User

The first time you touch LINQ is when using C #, this syntax is very handy when working with list data. If you want to know more about LINQ, you can get a bit of LINQ, but it doesn't matter if you don't understand it, let me show you the Glamour of LINQ in Lua. To put it simply, LINQ is the part that lets you ignore the loops and make you more professional-related business implementations. It's kind of a grammar candy.

Give me a few examples.

1. Output all content in a class

LocalTest1 ={Attribute1="Attribute1", Attribute2="Attribute2", Attribute3="Attribute3",}Print(String.Format("{%s}",Table.concat(Linq:linq (GetAttributes (test1)): Select (function(n)return String.Format("%s:%s", N.K, N.V)End),", "))) 

Output:

{attribute2:attribute2, attribute1:attribute1, attribute3:attribute3}

2. The ID in the Extract list data is assembled into a new ID list

LocalTest2 ={{id=1, name="Test2_1", Value="Test2_1",}, {ID=2, name="test2_2", Value="test2_2",}, {ID=3, name="Test2_3", Value="Test2_3",}, {ID=4, name="Test2_4", Value="Test2_4",}, {ID=5, name="Test2_5", Value="Test2_5",}, {ID=6, name="Test2_6", Value="Test2_6",    },  }Print(Table.concat(Linq:linq (test2): Select (function(n)returnN.idEnd),", "))

Output:

1, 2, 3, 4, 5, 6

3. Extract the contents of the list with the singular ID and assemble it into a new list

Localtemp = Linq:linq (test2): Where (function(n)returnN.id%2==1 End) forI, iteminch Ipairs(temp) Do    Print(String.Format("{%s}",Table.concat(Linq:linq (GetAttributes (item)): Select (function(n)return String.Format("%s:%s", N.K, N.V)End),", ")))End

Output:

{id:1, name:test2_1, Value:test2_1}{id:3, Name:test2_3, Value:test2_3}{id:5, Name:test2_5, value:test2_5} 

4. Assemble a table from the dictionary and return the data if all keys are present, otherwise return nil

LocalTest3 ={Key1="Key1", Key2="Key2", Key3="Key3", Key4="Key4",  }LocalKeys1 = {"Key1","Key2","Key3"}LocalKeys2 = {"Key1","Key2","Key5"}LocalTemp1 = {}Localall = Linq:linq (KEYS1): All (function(n)Localitem =Test3[n]ifItem ThenTemp1[n]=ItemEnd    returnItem ~=NilEnd)ifAll Then    Print(String.Format("key1 {%s}",Table.concat(Linq:linq (GetAttributes (TEMP1)): Select (function(n)return String.Format("%s:%s", N.K, N.V)End),", ")))Else    Print("Key1 Error")EndLocalall = Linq:linq (KEYS2): All (function(n)Localitem =Test3[n]ifItem ThenTemp1[n]=ItemEnd    returnItem ~=NilEnd)ifAll Then    Print(String.Format("key2 {%s}",Table.concat(Linq:linq (GetAttributes (TEMP1)): Select (function(n)return String.Format("%s:%s", N.K, N.V)End),", ")))Else    Print("Key2 Error")End

Output:

Key1 {key1:key1, Key3:key3, Key2:key2}key2 Error

Review

There are many uses for LINQ, and you need to use it with your imagination, and this place will not be too much to repeat. Let me briefly explain the methods that can be used.

Functions that can be used

Linq:linq

A table of the list type is passed in, and subsequent operations are based on the list type. The method content will clone a copy of the passed object, and will not destroy the original data. So be sure to use it boldly, though, LINQ supports passing the list class in, but I still don't recommend it.

Select

This function creates a new list of LINQ code additions. You need to pass in a function when calling this function, and LINQ passes each item of data in the list as a parameter, and the return value of the function is inserted into the new list. Simply put. This function is the way to convert the original list object to a new list object.

Where

The filter function. When you call this function, you need to pass in a function, LINQ lists the data, pass in the function in turn, and determine the return value of the function as a bool value, whether the object is added to the new list and returned.

Sum

Sum function, when calling this function, you need to break into a function, LINQ will put the data in your report, the function is passed in order, and the return value of the function is summed up to return.

All

A decision function that determines whether all objects in the list object have passed the checksum. When calling this function, it is necessary to pass in a checksum function, LINQ will pass each item in the list to the checksum function, and accept the check result, if any one of the decision fails, then return the failure directly, otherwise the return succeeds.

Any

A decision function that determines whether any object in the list object passes a checksum. When calling this function, it is necessary to pass in a checksum function, and LINQ will pass each item in the list to the check function, and if any one of the checks results is successful, the return succeeds or the failure is returned.

First

Gets the function that gets the first item of data. When this function is called, a checksum function can be passed in, and if there is a check function, it is returned as the result of verifying the success of the first item.

Get

Gets the function that gets the data for a specific object. The object for this function is not a list, but a validation object. Gets the name property of the checksum object.

Sort

Sort function, passing in a sort function that accepts two values. The function returns the sorted data.

Clear

Clean up functions, Clean up LINQ methods in your data

Questions

1. Will using LINQ destroy the original data?

No, because all the data is from the new clone, is the data after the operation of the clone, so there is no impact on the original data

2. Will using LINQ degrade performance?

But the impact should be small. The main factors affecting the performance are as follows: 1, Clone, does affect some, but for data security, this is still very necessary, and the use of LINQ is generally used to deal with the initialization of data, so low performance is not related; 2, the cycle of the list, I think this should not be counted as the main reason, because you have to do the list processing, must use the loop, there are loops, so this does not matter;

At last

LINQ is just a thought of working with list data, and using this list of data processing is relatively straightforward and only concerns the business logic that is directly relevant. It's easier to write. Anyway, I like this way better.

Related test Project address: http://git.oschina.net/anxin1225/LuaLinq

Let Lua support LINQ.

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.