In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to install and use MoreLinq". In daily operation, I believe many people have doubts about how to install and use MoreLinq. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to install and use MoreLinq". Next, please follow the editor to study!
One: background 1. Tell a story
A few days ago, I saw that my colleague was using linq to make a left connection to two model in memory. Friends who have used it all know that you must have a function called DefaultIfEmpty. This thing, originally very smooth from...in...join, suddenly got into such a function. It was really disgusting. His mother opened the door for disgusting. It was disgusting. The simplified code is as follows:
Class User {public int UserID {get; set;} public string Email {get; set;}} class Order {public int OrderID {get; set;} public string OrderTitle {get; set;} public int UserID {get; set } static void Main (string [] args) {var userList = new List () {new User () {UserID=1, Email= "333@qq.com"}, new User () {UserID=2, Email= "444@qq.com"},} Var orderList = new List () {new Order () {OrderID=1, OrderTitle= "order 1", UserID=1}, new Order () {OrderID=2, OrderTitle= "order 2", UserID=1}} Var query = from u in userList join oin orderList on u.UserID equals o.UserID into grp from item in grp.DefaultIfEmpty () select new {UserID = u.UserID, OrderTitle = item?.OrderTitle};}
The reason for this is that both query keywords and expansion methods do not directly support leftjoin and rightjoin, but can only be implemented in a flexible way, which is very embarrassing. For example, in the above code, it is difficult for you to accurately understand what the DefaultIfEmpty is used for many days later, so the crux of the problem now is Is there any way to get the underlying support or have a trusted third party to seal it for me? coincidentally, there is really a morelinq project on github: https://github.com/morelinq/MoreLINQ, look at md is to extend 60 Enumerable 70 methods, like a tiger to add wings, let's explore it next.
Second: exploration under MoreLinq 1. Installation
The installation is very simple, just run Install-Package morelinq-Version 3.3.2 with nuget.
2. LeftJoin / RightJoin / FullJoin
In morelinq, it provides left outside, right side, full connection, which is very? To improve the development efficiency and let's focus more on the business, here we use leftjoin to modify the code as follows:
Var query2 = userList.LeftJoin (orderList, u = > u.UserID, o = > o.UserID, u = > new {UserID = u.UserID, OrderTitle = default (string)}, (u, o) = > new {UserID = u.UserID, OrderTitle = o.OrderTitle})
Well, it's done with a chain, and the code is very easy to understand.
First parameter: join collection
Second parameter: the associated id of the source collection
The third parameter: the associated id of the join collection
Fourth parameter: source exists, join does not have the model schema that should be returned
The fifth parameter: source,join has the model schema that should be returned.
Just now I also mentioned that there are 70 expansion methods for 60JI. You can go to the party and have a party with other interesting things. Here I recommend a few to show you. After all, they are still very practical.
3. Shuffle
Literally, shuffle is a practical method. Very often, I want to randomly select a record from a collection. For example, I have 10 sets of email templates. When a user places an order, I hope to randomly select a set of templates for the user to prevent QQ Mail from putting them into the dustbin. It turns out that I need to use new guid to implement it, as follows:
Var list = new List () {1,3,5,7 9,11,12}; var query = list.OrderBy (m = > Guid.NewGuid ()); Console.WriteLine (string.Join (",", query))
It's much easier now, just use Shuffle to do it.
Var list = new List () {1, 3, 5, 7, 9, 11, 12}; var query = list.Shuffle (); Console.WriteLine (string.Join (",", query));-output-5. Insert
Sometimes I need to insert a collection in the specified location of the IEnumerable collection, for example, A = {1, 3, 5, 7, 9, 11, 12}, B = {8}. I want to insert 8 between 7 and 9. Under the existing Enumerable, there are only Concat and Append methods, which cannot be specified, and this requirement can be solved by moreqlinq again. The code is as follows:
IEnumerable list = new List () {1,3,5,7,9,11,12}; var query = list.Insert (new List () {8}, 4); Console.WriteLine (string.Join (",", query))
You can see that inserting a collection into a specified location of a collection is so easy and enjoyable.
5. ForEach
For some reason, no ForEach extension method has been provided under IEnumerable so far. I just don't understand the philosophy of such a design. In the past, you can only ToList to execute immediately, or use foreach for deferred traversal. Now it is much more convenient. The simplified code is as follows:
IEnumerable list = new List () {1,3,5,7,9,11,12}; list.ForEach (m = > {Console.Write (m + ");})
6. ToDataTable
It is estimated that there is no need for all the bosses who are watching. For several 10 +-year projects in our company, it is absolutely necessary. If any can be used, you can take a brief look at it:
It's very simple, but I'm curious about how it does it. You can use ilspy to flip through its source code:
Public static TTable ToDataTable (this IEnumerable source, TTable table, params Expression [] expressions) where TTable: DataTable {MemberInfo [] members = PrepareMemberInfos (expressions). ToArray (); members = BuildOrBindSchema (table, members); Func func = CreateShredder (members); table.BeginLoadData (); try {foreach (T item in source) {DataRow dataRow = table.NewRow () DataRow.ItemArray = func (item); table.Rows.Add (dataRow);} return table;} finally {table.EndLoadData () }} private static IEnumerable PrepareMemberInfos (ICollection expressions) {if (expressions = = null | | expressions.Count = = 0) {return typeof (T) .GetMembers (BindingFlags.Instance | BindingFlags.Public) .Where (delegate (MemberInfo m) {if (m.MemberType! = MemberTypes.Field) { PropertyInfo propertyInfo = m as PropertyInfo If ((object) propertyInfo! = null & & propertyInfo.CanRead) {return propertyInfo.GetIndexParameters (). Length = = 0;} return false } return true;});} try {return expressions.Select (GetAccessedMember);} catch (ArgumentException innerException) {throw new ArgumentException ("One of the supplied expressions is not allowed.", "expressions", innerException) } MemberInfo GetAccessedMember (LambdaExpression lambda) {Expression expression = lambda.Body; if (expression.NodeType = = ExpressionType.Convert | | expression.NodeType = = ExpressionType.ConvertChecked) {expression = ((UnaryExpression) expression) .Operand;} MemberExpression memberExpression = expression as MemberExpression If (memberExpression = = null | | memberExpression.Expression.NodeType! = ExpressionType.Parameter) {throw new ArgumentException ($"Illegal expression: {lambda}", "lambda");} return memberExpression.Member;}}
As you can see from the above source code, converting List to DataTable supports two ways, either reflection or Expression parsing tree. The default is reflection, and the performance is slightly lower.
At this point, the study on "how to install and use MoreLinq" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.