CS4540 Special Topics in Web Development LINQ to Objects

Slides:



Advertisements
Similar presentations
Developer Knowledge Sharing Eric Sun Dec, What programming language did you learn in school and since then? Now, its time to refresh …
Advertisements

The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd
Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training
LINQ and Collections An introduction to LINQ and Collections.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Svetlin Nakov Telerik Corporation
.NET 3.5 – Mysteries. NetFx Evolution NetFx 1.0 C# 1.0, VB 7.0, VS.NET NetFx 1.1 C# 1.1, VB 7.1, VS 2003 NetFx 2.0 C# 2.0, VB 8.0, VS 2005 NetFx 3.0 C#
C# and LINQ Yuan Yu Microsoft Research Silicon Valley.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions.
C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College
A tour of new features introducing LINQ. Agenda of LINQ Presentation We have features for every step of the way LINQ Fundamentals Anonymous Functions/Lambda.
XML files (with LINQ). Introduction to LINQ ( Language Integrated Query ) C#’s new LINQ capabilities allow you to write query expressions that retrieve.
LINQ Programming in C# LINQ CSE Prof. Roger Crawfis.
 Introduction  What is LINQ  Syntax  How to Query  Example Program.
LINQ, An IntroLINQ, An Intro Florin−Tudor Cristea, Microsoft Student Partner.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 11 th Lecture Pavel Ježek
 Continue queries ◦ You completed two tutorials with step-by-step instructions for creating queries in MS Access. ◦ Now must apply knowledge and skills.
Advanced C#, part IV Niels Hallenberg IT University of Copenhagen (With thanks to Peter Sestoft and Kasper Østerbye) BAAAP – Spring 2009.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
Introduction to LINQ Lecture # 19 August Introduction How do you interrogate/manipulate data? What if you could do the work in a type-safe," string-free.
CS1100: Microsoft Access Managing Data in Relational Databases Created By Martin Schedlbauer CS11001Microsoft Access - Introduction.
 Although VERY commonly used, arrays have limited capabilities  A List is similar to an array but provides additional functionality, such as dynamic.
Introduction to LINQ Chapter 11. Introduction Large amounts of data are often stored in a database—an organized collection of data. A database management.
C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
Joel Pobar Language Geek Microsoft DEV320 Improve on C# % Backwards Compatible Language Integrated Query (LINQ)
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
Language Integrated Query Mike Taulty Developer & Platform Group Microsoft Ltd
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Chapter 11.  Large amounts of data are often stored in a database—an organized collection of data.  A database management system (DBMS) provides mechanisms.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
Functional Programming Data Aggregation and Nested Queries Ivan Yonkov Technical Trainer Software University
IFS180 Intro. to Data Management Chapter 10 - Unions.
Introduction to.NET Florin Olariu “Alexandru Ioan Cuza”, University of Iai Department of Computer Science.
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming More SQL
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2016
CS320 Web and Internet Programming SQL and MySQL
Introduction to LINQ and Generic Collections
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2013
Relational Algebra - Part 1
Intro to LINQ Part 2 – Concepts and LINQ to Objects
Language Integrated Query: (LINQ) An introduction
Visual Basic 2010 How to Program
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Introduction to LINQ Chapter 11 10/28/2015 Lect 4 CT1411.
Introduction to LINQ Chapter 11.
Chapter 4 Summary Query.
Chengyu Sun California State University, Los Angeles
Chapter 2: Intro to Relational Model
CS3220 Web and Internet Programming SQL and MySQL
Topic 12 Lesson 2 – Retrieving Data with Queries
INFO 344 Web Tools And Development
CS3220 Web and Internet Programming SQL and MySQL
LINQ - 2 Ravi Kumar C++/C# Team.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
LINQ to SQL Part 3.
Advanced .NET Programming I 6th Lecture
Chengyu Sun California State University, Los Angeles
CS4540 Special Topics in Web Development SQL and MS SQL
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Presentation transcript:

CS4540 Special Topics in Web Development LINQ to Objects Chengyu Sun California State University, Los Angeles

C# Language Features Used in LINQ Implicit typing using var Anonymous types and tuples Extension methods Delegates and lambda expressions

Example: Find Data in Array List in the names that start with "J" in alphabetic order var results = new List<string>(); foreach (var name in names) if (name.StartsWith("J")) results.Add(name); results.Sort();

Using LINQ (Language Integrated Query) var results = from name in names where name.StartsWith("J") orderby name select name; OR var results = names .Where(name => name.StartsWith("J")) .OrderBy(name => name) .Select(name => name);

About LINQ … Query and transform data using SQL-like syntax Simplify coding Standardize data access across objects, relational data and XML data LINQ to Objects works on anything that implements IEnumerable, i.e. arrays and all collections

… About LINQ Query result is something that implements IEnumerable Using var is usually necessary

LINQ vs SQL LINQ is for objects while SQL is for relational data (i.e. rows and column) Object Collection from name in names where name.StartsWith("J") orderby name select name; Object Method OrderBy and projection on the whole object or some properties

Query Syntax vs. Method Syntax They are equivalent (query expressions are eventually translated into extension methods) Query syntax tends to be more concise and readable Query syntax does not cover all extension methods (i.e. sometimes you have to use method syntax)

Query Execution Deferred execution: LINQ queries not executed until the iteration of the results Getting the latest result Repeated execution Immediate execution can be done by calling ToArray() or ToList() Query operators that return a single value (e.g. Count()) also execute immediately

Basic LINQ Query Syntax An arbitrary name for an item in the collection The name of the collection from <item> in <collection> where <conditions> select <projection> Conditions like in SQL but expressed using C# operators and methods The whole object or some of its properties (using anonymous type or tuple syntax)

Standard Query Operators Implemented as extension methods in System.Linq.Enumerable Only some of the extension methods have equivalent query expression

The Company Example Employee Project Company

Simple Selection Q1. Find the employees whose last names are Doe With order by With projection With duplicate removal

The Difference Between Objects and Relational Data Q2. find the leader of the project Blue SQL: JOIN LINQ: ??

Joins Q3. Find Jane Doe's supervisor Query syntax: from <item1> in <collection1> join <item2> in <collection2> on <key1> equals <key2> Method syntax: ??

Set Operations Q4. Find the employees who are on both project Firestone and project Blue Set operations: intersect, except, union Element operations

Aggregation Functions 5. Find the number of employees hired in 2015 Aggregation functions: count, sum, average, min, max

GroupBy in LINQ … 6. Group the employees by the year in which they were hired Unlike in SQL, GroupBy in LINQ only creates groups without requiring aggregation Each group is represented by an IGrouping<TKey, Telements>

… GroupBy in LINQ Query syntax: from <item> in <collection> group <element> by <key> Method syntax: ??

Simple Query Builder by Chaining Where() … Employee Search First Name Last Name Year Hired Search

… Simple Query Builder By Chaining Where() IEnumerable<Employee> results = c.Employees; if (!String.IsNullOrEmpty(firstName)) results = results.Where( e => e.FirstName == firstName); if (!String.IsNullOrEmpty(lastName)) e => e.LastName == lastName); if (!String.IsNullOrEmpty(year)) e => e.DateHired.Year == Int32.Parse(year)); results = results.Select(e => e);

Readings Pro C# 7: Chapter 12