Introduction to LINQ With C#

Language

Introduction to LINQ Integrating data queries into c# has been a goal for years. Microsoft put lot of effort to such a goal over the years. At lat outcome is LINQ (Language Integrated Queries). Using LINQ we can specify what object we want without knowing the type of data source. There are two type of method usage in LINQ. Static method represents the query operators and anonymous method specifies the predicate. What are the different types of data? Development of software is huge challenge for development team since they have to deal with number of data sources. They need to know each of data source API within life time of the project. Using number of data sources within code will make development much harder and delay the development. Following are some of typical data type challengers Different data type Different data sources Relationship with hierarchies Limited intellisense or language What is the motivation of LINQ? LINQ is a new idea and unique API which provide data access easier and enable queering capabilities to more power. Generally data sources determine tools and API to use. As e.g. object data, relation data, XML data. If someone is working with these API he will need better understanding how to dealing with data and API. Greatest achievement of LINQ is, LINQ can be used to query many different types of data, including relational, XML, and even objects. Another way to describe LINQ is that it is programming language syntax that is used to query data. In this tutorial I will guide you few example of LINQ. I am here not going to teach you all the LINQ Technologies. Mainly focus on to explain few xml handling with LINQ. Let say we have xml file called customer.xml and it has following structure
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <customers>
  3. <customer name="steven">
  4. <id>011475</id>
  5. <fname>steven</fname>
  6. <age>36</age>
  7. <city>newyork</city>
  8. </customer>
  9. <customer name="julian">
  10. <id>52485</id>
  11. <fname>julian</fname>
  12. <age>22</age>
  13. <city>london</city>
  14. </customer>
  15. <customer name="dev">
  16. <id>5485</id>
  17. <fname>dev</fname>
  18. <age>26</age>
  19. <city>sydny</city>
  20. </customer>
  21. <customer name="ben">
  22. <id>485214</id>
  23. <fname>ben</fname>
  24. <age>36</age>
  25. <city>sydny</city>
  26. </customer>
  27. <customer name="john">
  28. <id>24586</id>
  29. <fname>John</fname>
  30. <age>42</age>
  31. <city>london</city>
  32. </customer>
  33. </customers>
Following c# code shows how to use LINQ in C# how to load data to combo box and data grid How to load document from path
  1. XDocument xdoc = XDocument.Load("D:\\customers.xml");
Few data bindings
  1. // list box binding
  2.  
  3. var customes = from customer in xdoc.Descendants("customers").Elements("customer").Attributes("name")
  4. select customer.Value;
  5.  
  6. cmbCustomer.DataSource = customes.ToList();
  7.  
  8. // Grid binding
  9.  
  10. var customesList = from customers in xdoc.Root.Elements()
  11. select new {
  12. name = customers.Element("fname").Value,
  13. id = customers.Element("id").Value,
  14. age = customers.Element("age").Value,
  15. city = customers.Element("city").Value
  16. };
  17.  
  18. grdCustomer.DataSource = customesList.ToList();
Following code shows you how to used where in XML document
  1. var whereList = from customers in xdoc.Root.Elements()
  2. where customers.Element("city").Value == "london"
  3. select new
  4. {
  5. name = customers.Element("fname").Value,
  6. city = customers.Element("city").Value
  7. };
  8.  
  9. grdWhere.DataSource = whereList.ToList();
linq I will explain object data handling with LINQ in my next LINQ tutorial

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment