Saturday, 13 October 2012

c# for dummies: c sharp classes

c# for dummies: c sharp classes: USING LINQ namespace LINQDev.HR {   public class Employee   {     public int id;     public string firstName;     public string lastName;   ...

c sharp classes

USING LINQ

namespace LINQDev.HR
{
  public class Employee
  {
    public int id;
    public string firstName;
    public string lastName;

    public static ArrayList GetEmployees()
    {
      //  Of course the real code would probably be making a database query
      //  right about here.
      ArrayList al = new ArrayList();

      //  Man, do the C# object initialization features make this a snap.
      al.Add(new Employee { id = 1, firstName = "Joe", lastName = "Rattz"} );
      al.Add(new Employee { id = 2, firstName = "William", lastName = "Gates"} );
      al.Add(new Employee { id = 3, firstName = "Anders", lastName = "Hejlsberg"}
);
      return(al);
    }
  }
}




namespace LINQDev.Common
{
  public class Contact
  {
    public int Id;
    public string Name;

    public static void PublishContacts(Contact[] contacts)
    {
      //  This publish method just writes them to the console window.
      foreach(Contact c in contacts)
        Console.WriteLine("Contact Id:  {0}  Contact:  {1}", c.Id, c.Name);
    }
  }
}
\


LINQDev.Common.Contact[] contacts = alEmployees
  .Cast<LINQDev.HR.Employee>()
  .Select(e => new LINQDev.Common.Contact {
                 Id = e.id,
                 Name = string.Format("{0} {1}", e.firstName, e.lastName)
               })
  .ToArray<LINQDev.Common.Contact>();

LINQDev.Common.Contact.PublishContacts(contacts);


OUTPUT:

Contact Id:  1  Contact:  Joe Rattz
Contact Id:  2  Contact:  William Gates
Contact Id:  3  Contact:  Anders Hejlsberg

c# for dummies: c sharp programming

c# for dummies: c sharp programming: HOW TO QUERY LINQ TO SQL DATABASE using System; using System.Linq; using System.Data.Linq; using nwind; Northwind db = new Northwind(@"Data ...

c sharp programming

HOW TO QUERY LINQ TO SQL DATABASE


using System;
using System.Linq;
using System.Data.Linq;

using nwind;

Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial
Catalog=Northwind");

var custs =
  from c in db.Customers
  where c.City == "Rio de Janeiro"
  select c;

foreach (var cust in custs)
  Console.WriteLine("{0}", cust.CompanyName);

c# for dummies: c sharp classes

c# for dummies: c sharp classes: HOW TO QUERY  XML USING LINQ using System; using System.Linq; using System.Xml.Linq; XElement books = XElement.Parse(   @"       <boo...

c sharp classes

HOW TO QUERY  XML USING LINQ


using System;
using System.Linq;
using System.Xml.Linq;


XElement books = XElement.Parse(
  @"<books>
      <book>
          <title>Pro LINQ: Language Integrated Query in C# 2010</title>
          <author>Joe Rattz</author>
      </book>
      <book>
          <title>Pro .NET 4.0 Parallel Programming in C#</title>
          <author>Adam Freeman</author>
      </book>
      <book>
          <title>Pro VB 2010 and the .NET 4.0 Platform</title>
          <author>Andrew Troelsen</author>
       </book>
    </books>");

var titles =
  from book in books.Elements("book")
  where (string) book.Element("author") == "Joe Rattz"
  select book.Element("title");

foreach(var title in titles)
  Console.WriteLine(title.Value);

c# for dummies: c sharp programming

c# for dummies: c sharp programming: how to use linq using System; using System.Linq;   string[] greetings = {"hello world", "hello LINQ", "hello Apress"}; var items =    fr...