Real-world applications

Source: Internet
Author: User

Simple sum
Int [] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
Double numsum = numbers. sum ();

 

Total characters in the array
String [] words = {"Cherry", "apple", "blueberry "};
Double totalchars = words. sum (W => W. Length );

 

Find the minimum value
Int [] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
Int minnum = numbers. Min ();

 

Find the string with the least number of characters
String [] words = {"Cherry", "apple", "blueberry "};
Int shortestword = words. Min (W => W. Length );

Check whether there is at least one EI value in the array.
String [] words = {"believe", "relief", "receipt", "field "};
Bool iaftere = words. Any (W => W. Contains ("EI "));

 

Retrieve and sort the unique values of the Name field in the table
Datatable orders = dataset. Tables [0];
Enumerablerowcollection <datarow> query = (from order in orders. asenumerable () orderby order. field <string> ("name") Select order. field <string> ("name ")). distinct ();
Dataview view = query. asdataview ();
Gridview1.datasource = view;
Gridview1.databind ();

 

Group by name field in the table, total money
Datatable orders = dataset. Tables [0];
VaR query = from order in orders. asenumerable () group order by order. field <string> ("name") into G select new {names = G. key, moneys = G. sum (Order => order. field <decimal> ("money "))};
Gridview1.datasource = query. asqueryable ();
Gridview1.databind ();

 

Internal Connection
Datatable orders = dataset. Tables [0];
Datatable citys = dataset. Tables [1];
VaR query = from order in orders. asenumerable ()
          Join city in Citys. asenumerable ()
          On order. Field <string> ("name") equals city. Field <string> ("name ")
          Select New
          {
              Name = order. Field <string> ("name "),
              Money = order. Field <decimal> ("money "),
              City = city. Field <string> ("city ")
          };

 

Left join
Datatable orders = dataset. Tables [0];
Datatable citys = dataset. Tables [1];
VaR query = from order in orders. asenumerable ()
          Join city in Citys. asenumerable ()
          On order. Field <string> ("name") equals city. Field <string> ("name") into CITs
          Select New
          {
              Name = order. Field <string> ("name "),
              Money = order. Field <decimal> ("money "),
              City = CITS. Count ()> 0? CITS. First (). Field <string> ("city "):""
          };

 

Merge and remove duplicate items
Int [] S1 = {1, 2, 3, 4, 5 };
Int [] S2 = {3, 2, 7, 8, 10 };

VaR s3s = s1.union (S2 );
Foreach (VAR S3 in s3s)
 MessageBox. Show (s3.tostring ());

Add the drag table transtest by creating a new item of the LINQ to SQL class
Modification records with transactions
Dataclassesdatacontext DB = new dataclassesdatacontext (configurationmanager. connectionstrings ["testdbconnectionstring"]. connectionstring );
DB. commandtimeout = 60;
Using (transactionscope Ts = new transactionscope ())
{
 VaR records = dB. transtest. Where (P => P. Name = "Maj 'ie ");
 Foreach (VAR record in Records)
 {
   Record. Orders = 1;
   Record. Other = datetime. Now. Second. tostring ();
 }
 DB. submitchanges ();
 TS. Complete ();
}

Add record
Dataclassesdatacontext DB = new dataclassesdatacontext (configurationmanager. connectionstrings ["testdbconnectionstring"]. connectionstring );
Transtest record = new transtest ();
Record. Other = "Iloveyou ";
DB. transtest. insertonsubmit (record );
DB. submitchanges ();

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.