Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Thursday, 9 April 2015

How to access Web Site through SharePoint Client Object Model

You can access WebSite  Properties through SharePoint Client Object Model.
Microsoft.SharePoint.Client should be use.


Code: 
ClientContext clientContext = new ClientContext("Site URL");

NetworkCredential Cred = new NetworkCredential("UserName""Password", "Domain");
clientContext.Credentials = Cred;
Web web = clientContext.Web;

clientContext.Load(web);
clientContext.ExecuteQuery();

Console.WriteLine("Title:" + web.Title);
Console.WriteLine("Description:" + web.Description);
Console.WriteLine("Created:" + web.Created);
onsole.ReadLine();



If you want to access only specific properties from WebSite, it can be achieved using LINQ.

Code:

ClientContext clientContext = new ClientContext("Site URL");

NetworkCredential Cred = new NetworkCredential("UserName""Password""Domain");
clientContext.Credentials = Cred;
Web web = clientContext.Web;

clientContext.Load(web, a => web.Title,
                        a => web.Description,
                        a => web.Created);clientContext.ExecuteQuery();

Console.WriteLine("Title:" + web.Title);
Console.WriteLine("Description:" + web.Description);
Console.WriteLine("Created:" + web.Created);

onsole.ReadLine();

Friday, 17 August 2012

LINQ To ADO.Net


There are three separate ADO.NET Language-Integrated Query (LINQ) technologies: LINQ to DataSet, LINQ to SQL, and LINQ to Entities. 

LINQ to DataSet provides richer, optimized querying over the DataSet


LINQ to SQL enables you to directly query SQL Server database schemas, and 


LINQ to Entities allows you to query an Entity Data Model.



  LINQ to DataSet

The Dataset is one of the most widely used components in ADO.NET, and is a key  element of the disconnected programming model that ADO.NET is built on. Despite this prominence, however, the Dataset  has  limited query capabilities. 

LINQ to DataSet enables you to build richer query capabilities into DataSet by using the same query functionality that is available for many other data sources

LINQ to SQL

LINQ to SQL provides a run-time infrastructure for managing relational data as objects. In LINQ to SQL,  the data model of a relational database is mapped to an object model expressed in the programming language of the developer. When you execute the application, LINQ to SQL translates language-integrated queries in the object model into SQL and sends them to the database for execution. When the database returns the results, LINQ to SQL translates them back into objects that you can manipulate.
LINQ to SQL includes support for stored procedures and user-defined functions in the database, and for inheritance in the object model.

LINQ to Entities


Through the Entity Data Model, relational data is exposed as objects in the .NET environment. This makes the object layer an ideal target for LINQ support, allowing developers to formulate queries against the database from the language used to build the business logic.

LINQ To Objects


 

  • LINQ to Objects refer to the use of LINQ queries with any IEnumerable or IEnumerable<T> collection.
  • LINQ to query any enumerable collections such as List<T>, Array, or Dictionary<TKey, TValue>
  • LINQ to Objects represents a new approach to collections. In older way, you had to write complex foreach loops that specified how to retrieve data from a collection.
  •  LINQ queries offer three main advantages over traditional foreach loops:
    1. They are more concise and readable, especially when filtering multiple conditions.
    2. They provide powerful filtering, ordering, and grouping capabilities with a minimum of application code.
    3. They can be ported to other data sources with little or no modification.

How to: Query an ArrayList with LINQ

LINQ to query non-generic IEnumerable collections such as ArrayList, you must explicitly declare the type of the range variable to reflect the specific type of the objects in the collection.

Here I have taken Cricket 's Player class to understand the Linq to Object.

Player contains single properties FirstName,LastName,Country and Multivalue Properties like Scores.List of batsman who have scrored more than 50 runs in their First Match

public class Player
   {
       public string FirstName { get; set; }
       public string LastName { get; set; }
       public string Country { get; set; }
       public int[] Scores { get; set; }
       public int ManOfTheSeries { get; set; }
 
   }
   class Program
   {
       static void Main(string[] args)
       {
           IList<Player> playerList = new List<Player>()
           {
               new Player
               {
                   FirstName = "Sachin",
                   LastName = "Tendulkar",
                   Country= "India",
                   Scores = new int[] { 200, 190, 170,150  }
               },
 
      
               new Player
               {
                   FirstName = "Ricky",
                   LastName = "Ponting",
                   Country = "Australia",
                   Scores = new int[] { 180, 84, 70, 142 }
               },
        
               new Player
               {
                   FirstName = "Kumar",
                   LastName = "Sangakara",
                   Country = "Sri Lanka",
                   Scores = new int[] { 5, 80, 5, 39 }
               },
          
               new Player
               {
                   FirstName = "Hashim",
                   LastName = "Amla",
                   Country = "South Africa",
                   Scores = new int[] { 88, 94, 65, 91 }
               },
          
               new Player
               {
                   FirstName = "Mahela",
                   LastName = "Jayawardene",
                   Country = "Sri Lanka",
                   Scores = new int[] { 97, 89, 85, 82 }
               }
 
           };
       
       
           var query = from Player player in playerList
                       where player.Scores[0] >50
                       select player;
         
           Console.WriteLine("List of batsman who have scrored more than 50 runs in their First Match");
           foreach (Player s in query)
               Console.WriteLine(s.FirstName +" " + s.LastName + ": " + s.Scores[0]);
        
            Console.WriteLine("Press any key to exit.");
           Console.ReadKey();
       } 
       }

Output
Sachin Tendulkar: 200
Ricky Ponting: 180
Hashim Amla: 88
Mahela Jayawardene: 97
Press any key to exit.

To get same output with query operator,

Replace above query part with below code.





Console.WriteLine("FILTER- List of batsman who have scrored more than 50 runs in their First Match");
          var queryFilter = playerList
                           .Where(a => a.Scores[0] > 50)
                           .Select(a => a);



 Linq And String


http://msdn.microsoft.com/en-us/library/bb397915.aspx

LINQ and File Directories

http://msdn.microsoft.com/en-us/library/bb397911.aspx

LINQ and Reflection

http://msdn.microsoft.com/en-us/library/bb397929.aspx

How to: Add Custom Methods for LINQ Queries

http://msdn.microsoft.com/en-us/library/cc981895.aspx

Tuesday, 14 August 2012

LINQ - .Net Framework


Overview

  • Linq is uniform programming model which can interact with any kind of Data access like access,xml , SQL server, Oracle. 
  • Linq enables you to query and manipulate the data independently of data sources and any transformation. It is generalize interface which can query to any data source.
  •  Linq also provide type safety and any kind of data compile time checking.
  • It can serve as good entity for middle tier. So it is between UI and data access layer.
  • It provides declaratives syntax and enable developer to compile or provide what to do.
  • Hierarchical feature of LINQ allows you to easily see the relationship between tables, thereby making it easy to quickly compose queries that join multiple tables.
  • If you know Linq to object, it is easier to learn linq to SQL and Linq to XML
  • The composable feature of LINQ also makes it easy to break complex problems into a series of short, comprehensible queries that are easy to debug
  • Because LINQ is composable, you can easily join multiple data sources in a single query, or in a series of related queries
  • The compiler and provider translate declarative code into the code that is actually executed. As a rule, LINQ knows more than the average developer about how to write highly optimized, efficient code.


Entity Framework  - .Net Framework 4.5


Auto-Compiled LINQ Queries



When you write a LINQ to Entities query before .Net framework 4.5, the Entity Framework walks over the expression tree generated by the C#/Visual Basic compiler and translates (or compiles) that into SQL






Compiling the expression tree into SQL involves some overhead, though, particularly for more complex queries. In previous versions of the Entity Framework, if you wanted to avoid having to pay this performance penalty every time a LINQ query was executed, you had to use the CompiledQuery class.

This new version of the Entity Framework 4.5 supports a new feature called Auto-Compiled LINQ Queries. Now every LINQ to Entities query that you execute automatically gets compiled and placed in the Entity Framework query plan cache. Each additional time you run the query, the Entity Framework will find it in its query cache and won’t have to go through the whole compilation process again. 



Translate