Training 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

EAGER LOADING ENTITY FRAMEWORK

• Eager loading is the process whereby a query for one type of entity also
loads related entities as part of the query.
• So that we don't need to execute a separate query for related entities.
• Eager loading is achieved using the Include() method.

• It gets all the students from the database along with its standards using the
Include() method.
LINQ QUERY SYNTAX

using (var context = new SchoolDBEntities())


{
var stud1 = (from s in context.Students.Include("Standard")
where s.StudentName == "Bill"
select s).FirstOrDefault<Student>();
}
LINQ METHOD SYNTAX

using (var ctx = new SchoolDBEntities())


{
var stud1 = ctx.Students .Include("Standard")
.Where(s => s.StudentName == "Bill") .FirstOrDefault<Student>();
}
USE LAMBDA EXPRESSION

•We can also use the LINQ lambda expression as a parameter in the Include
method.
•take a reference of System.Data.Entity namespace and use the lambda
expression
USE LAMBDA EXPRESSION
using System;
using System.Data.Entity;
class Program
{ static void Main(string[] args)
{
using (var ctx = new SchoolDBEntities())
{
var stud1=ctx.Students.Include(s => s.Standard)
.Where(s=>s.StudentName=="Bill")
.FirstOrDefault<Student>();
}
}
}
LOAD MULTIPLE ENTITIES
using (var ctx = new SchoolDBEntities())
{
var stud1=ctx.Students.Include(s => s.Standard.Teachers)
.Where(s => s.StudentName ==
"Bill") .FirstOrDefault<Student>();

}
FIELDS

• A field is a variable that is a member of a class and can hold data of the
class. For example:
public class Student
{
string name;
int age = 10;
}
PROPERTIES
• Provide access to a class attribute (a field). Useful for exposing fields in
components.
• A property is declared like a field, but with a get/set block added.
public class Student
{
public string FirstName
{ get;
set;
}
}
METHODS

• Methods implement some action that can be performed by an object.


• Methods are the operations performed on the data.
• A method can receive input data from the caller by specifying parameters
and output data back to the caller by specifying a return type.
<accessibilityLevel><returnType><functionName>(paramType paramName, . . . . . . )
{
Logic here
}
CONTD…

public int Sum(int a, int b)


{
return a+b;
}
METHOD TYPES

• static: accessible through class name. not the object instance.


• virtual: Method may be overridden in derived class.
• abstract: Method must be overridden in non-abstract derived
class(permitted only on abstract class).
• override: Method overrides a base class method.
PARTIAL CLASSES

• Many developers need access to the same class, then having the class in
multiple files can be beneficial.
• The partial keywords allow a class to span multiple source files.
Eg.
public partial class partialclassDemo
{
}
STATIC CLASSES
• A static class is declared using the "static" keyword.
• If the class is declared as static then the compiler never creates an instance of the class.
• All the member fields, properties and functions must be declared as static and they are accessed by
the class name directly not by a class instance object.
public static class staticDemo
{
public static void Add(int a, int b)
{
return a+b;
}
}
• //function calling directly
staticDemo.Add();
ABSTRACT CLASSES

• C# allows both classes and functions to be declared abstract using the


abstract keyword.
• We can't create an instance of an abstract class.
• An abstract member has a signature but no function body and they must be
overridden in any non-abstract derived class.
public abstract class Employee
{
public abstract string displayData();
}
CONTD…

public class test : Employee


{
public override string displayData()
{
return “Abstract class method";
}
}
SEALED CLASSES

• Sealed classes cannot be inherited. You can create an instance of a sealed


class. A sealed class is used to prevent further refinement through
inheritance.
• sealed class SealedClass
{
void myfunv();
}
• public class test :SealedClass//wrong. will give compilation error
{
}
MULTIPLE INHERITANCE

• Multiple inheritance in .NET framework cannot be implemented with


classes, It can only be implemented with interfaces.
Interface
• An interface is a set of related functions that must be implemented in a
derived class.
• Members of an interface are implicitly public and abstract.
• Interfaces are similar to abstract classes.
• First, both types must be inherited; second, you cannot create an instance
of either.
CONTD…

• Although there are several differences as in the following;


• An Abstract class can contain some implementations but an interface can't.
• An Interface can only inherit other interfaces but abstract classes can
inherit from other classes and interfaces.
• An Abstract class can contain constructors and destructors but an interface
can't.
• An Abstract class contains fields but interfaces don't.
C# FEATURES
• lambda expressions(=>),LINQ , implicit typing (var), query expressions
Lambda Expression:
• Lambda expressions are anonymous functions that contain expressions or
sequence of operators.
• All lambda expressions use the lambda operator =>
• The left side of the lambda operator specifies the input parameters and the right
side holds an expression.
Eg. Parameter => expression
Parameter-list => expression
n => n % 2 == 0
CONTD…

• using System.Linq.Expressions;
• using System.Linq;
• List<int> numbers = new List<int>{11,37,52};
• List<int> oddNumbers = numbers.where(n => n % 2 == 1).ToList();
var keyword:
var a=10;
QUERY EXPRESSION(LINQ QUERIES)

• A query is an expression that retrieves data from a data source.


• A query is distinct from the results that it produces.
• Queries are usually expressed in a specialized query language.
• The application always sees the source data as an IEnumerable<T> or
IQueryable<T> collection.
• int[] scores = { 90, 71, 82, 93, 75, 82 };
• IEnumerable<int> scoreQuery = from score in scores where score > 80
orderby score descending select score;

You might also like