Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to simplify programming with LINQ combination query

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to simplify programming with LINQ combination query". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Initialize array

In general, we need to initialize the value of the array to the same value or an incremental sequence value, or it may be an increment / decrement sequence with steps not equal to 1. With the linq composite query, we can do all the work in the array initializer, eliminating the need for loops!

In the following example code, the * * line initializes an array of length 10, all elements are-1, the second line initializes b to 0, 1, 2 to 9, and the third line initializes c to 100, 110, 120 to 190.

Int [] a = enumerable.repeat (- 1,10). Toarray (); int [] b = enumerable.range (0,10). Toarray (); int [] c = enumerable.range (0,10). Select (I = > 100 + 10 * I). Toarray ()

A word of warning: if you initialize a large array, * doesn't consider this elegant way, but uses the traditional way instead. This solution of linq composite queries produces arrays dynamically, so garbage arrays need to be reclaimed at run time. That is, I always use this technique in decimal arrays or when testing debug code.

two。 Traverse multiple arrays in a loop

A friend asked me a question from C #: is there a way to traverse multiple collections in one loop? His code looks something like this:

Foreach (var x in array1) {dosomething (x);} foreach (var x in array2) {dosomething (x);}

In this way, the body of the loop will be large, and he doesn't want the code to be repeated in this way. However, he doesn't want to create an array to hold all the elements of array1 and array2.

Linq composite queries provide an elegant solution: concat operations. We can rewrite the above code using a single loop, as follows:

Foreach (var x in array1.concat (array2)) {dosomething (x);}

Note that because the linq composite query operates at the enumerator level, it does not produce a new array to hold the elements of array1 and array2. Therefore, in addition to elegance, this scheme is also very efficient.

3. Generate random sequences

This is a simple technique for generating n-length random sequences:

Random rand = new random (); var randomseq = enumerable.repeat (0, n) .select (I = > rand.next ())

With the delay feature of linq composite queries, sequences are not evaluated and saved to an array, but random numbers are generated as needed when iterating through randomseq.

4. Generate string

Linq composite queries are also a good tool for generating various types of strings. For testing or debugging, it is useful to generate strings. Suppose we need to generate an n-length string, in the form of "abcabcabc". Using linq composite queries, the solution is elegant:

String str = new string (enumerable.range (0, n) .select (I = > (char) ('a'+ I% 3)) .toarray ()

Petar petrov shows another interesting way to use linq composite queries to generate strings:

String values = string.join (string.empty, enumerable.repeat (pattern, n) .toarray ())

5. A conversion sequence or collection.

We cannot convert a sequence from type t to type u in c # or vb, even if t inherits from class u. So even if we convert list to list, if we need to convert list to list,linq composite query, it provides a solution, but it replicates the list:

List strlist =... ; listobjlist = new list (strlist.cast ())

Chris cavanagh suggests another solution:

Var objlist = strlist.cast () .tolist ()

6. Convert a value to a sequence of length 1

What do we do when we need to convert a single value into a sequence of length 1? We can create an array of length 1, but I still like the repeat operation of the linq composite query:

Ienumerable seq = enumerable.repeat (myvalue, 1)

7. All subsets of ergodic sequences

Sometimes it is useful to traverse all subsets of an array. The subset sum problem, the Boolean satisfiability problem, and the knapsack problem can all be solved simply by traversing all subsets of a sequence.

With the linq combination query, we can have the following subset of all the arr arrays of the sound field:

T [] arr =...; var subsets = from m in enumerable.range (0,1)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report