An example of the function of writing data to objects

Source: Internet
Author: User

Querying matching data in two sets and outputting the data can be easily written, which is a little shocking. If you don't study it, it's really outdated!

 

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace consoleapplication4
{
Class Program
{
Static void main (string [] ARGs)
{
List <product> List = new list <product> ();
List. Add (New Product ("HTC", 3000 ));
List. Add (New Product ("Nokia", 2000 ));
List. Add (New Product ("Moto", 1000 ));

List <company> company_list = new list <company> ();
Company_list.add (New Company ("HTC", "Taiwan "));
Company_list.add (New Company ("Nokia", "swiden "));
Company_list.add (New Company ("Moto", "America "));

 

var fliter = from product P in list
join company C1 in company_list on p. name equals c1.companyname
where p. pricing> 1000
select new {P. name, P. price, c1.companyaddress};

Foreach (var v in fliter)
{
Console. writeline (V. Name + v. companyaddress + v. Price );
}

Console. readkey ();
}
}

Public class product
{
Public string name {set; get ;}
Public decimal price {set; get ;}

Public Product (string name, decimal price)
{
This. Name = Name;
This. Price = price;
}
}

Public class company
{
Public String companyName {set; get ;}
Public String companyaddress {set; get ;}

Public Company (string _ name, string _ address)
{
This. companyName = _ name;
This. companyaddress = _ address;
}
}
}

 

**************************************** ********************

This collection Initialization is outdated.

List <product> List = new list <product> ();
List. Add (New Product ("HTC", 3000 ));
List. Add (New Product ("Nokia", 2000 ));
List. Add (New Product ("Moto", 1000 ));

List <company> company_list = new list <company> ();
Company_list.add (New Company ("HTC", "Taiwan "));
Company_list.add (New Company ("Nokia", "swiden "));
Company_list.add (New Company ("Moto", "America "));

 

It can be transformed as follows:

 

List <product> List = new list <product> (){
New product ("HTC", 3000 ),
New product ("Nokia", 2000 ),
New product ("motorcycle", 1000)
};

List company_list = new list ()
{< br> New Company ("HTC", "Taiwan "),
New Company ("Nokia", "swiden"),
New Company ("Moto", "America")
};

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.