0% found this document useful (0 votes)
19 views12 pages

LINQ and Entity Framework Examples

Uploaded by

jetsetgoflight
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views12 pages

LINQ and Entity Framework Examples

Uploaded by

jetsetgoflight
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

LINQ

_____________

1. Vehicle information

[Link]

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace Linq_1 //DO NOT change the namespace name


{
public class Program //DO NOT change the class name
{
public static List<Vehicle> VehicleDetails = new List<Vehicle>
{
new Vehicle { VehicleNo = "US", VehicleType = "2 Wheeler", Insured
= "Yes" },
new Vehicle { VehicleNo = "OH", VehicleType = "2 Wheeler", Insured
= "Yes" },
new Vehicle { VehicleNo = "CL", VehicleType = "4 Wheeler", Insured
= "No" },
new Vehicle { VehicleNo = "TX", VehicleType = "2 Wheeler", Insured
= "No" },
new Vehicle { VehicleNo = "CT", VehicleType = "4 Wheeler", Insured
= "Yes" },
new Vehicle { VehicleNo = "OR", VehicleType = "4 Wheeler", Insured
= "Yes" }
};

public static void Main(string[] args) //DO NOT change the method
signature
{
//Implement your code here
[Link]("Enter the vehicle type:");
string dd = [Link]();
[Link]("is it insured");
string dk = [Link]();
var ds = [Link](dd,dk);
}

//Implement the method here


public static List<Vehicle> DisplayVehicles (String vehicleType, string
insured)
{
var dd = [Link](a=>([Link]== vehicleType) &&
([Link] == insured)).ToList();
return dd;
}
public static ParameterExpression variableExpr =
[Link](typeof(List<Vehicle>), "sampleVar");
public static Expression GetMyExpression(String vehicleType, String
insured)
{
{
/** Copy ONLY the Query Expresion into the below comment area **/
Expression assignExpr = [Link](variableExpr,
[Link]([Link](a=>([Link]== vehicleType) &&
([Link] == insured)).ToList()));
return assignExpr;
}
}
}
}

[Link]

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Linq_1 //DO NOT change the namespace name


{
public class Vehicle //DO NOT change the class name
{
//Implement your code here
public string VehicleNo {get; set;}
public string VehicleType {get; set;}
public string Insured {get; set;}
}

___________________________________________________________________________

2. Inventory managment

[Link]

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace LINQ2 //DO NOT change the namespace name


{
public class Program //DO NOT change the class name
{
public static List<Inventory> InventoryList = new List<Inventory>()
{
new Inventory { ProductId = "MLT44", ProductName = "Mi LED TV 4A
43inches", Quantity = 21, ProductPrice = 22999.87},
new Inventory { ProductId = "S3SLMM", ProductName = "Samsung 32inches
Smart LED Monitor MS", Quantity = 17, ProductPrice = 25077.64},
new Inventory { ProductId = "A3SLFT", ProductName = "AmazonBasics
32inches Smart LED Fire TV", Quantity = 8, ProductPrice = 15898.50},
new Inventory { ProductId = "O3LTY", ProductName = "Oneplus 32inches
LED TV Y-Series", Quantity = 11, ProductPrice = 16356.98},
new Inventory { ProductId = "L5USLT", ProductName = "LG 55inches UHD
Smart LED TV", Quantity = 9, ProductPrice = 51866.90}
};

//Implement the methods here


public List<Inventory> GetProductById(String productId){
var cd =[Link](i => [Link] == productId).ToList();
return cd;
}
public List <Inventory> SortByProductPrice(){
var cd = [Link](i=> [Link]).ToList();
return cd;
}
public static void Main(string[] args) //DO NOT change the method
signature
{
//Implement your code here-
Program pp = new Program();

[Link]("1. Get product by Id\n 2. Sort by Product Price\n 3.


Exit");
[Link]("Enter your choice");
int n = [Link]([Link]());
if(n==1){
[Link]("Enter the product id");
String ab = [Link]();
var ps = [Link](ab);
}
if(n==2){
var cs = [Link]();
}
}

public static ParameterExpression variableExpr_GetProductById =


[Link](typeof(List<Inventory>), "sampleVar");
public static Expression GetMyExpression(String productId)
{
{
/** Copy ONLY the Query Expresion into the specified comment area
and return the expression **/
Expression assignExpr =
[Link](variableExpr_GetProductById,
[Link]([Link](i => [Link] == productId).ToList()));
return assignExpr;
}
}

public static ParameterExpression variableExpr_SortByProductPrice =


[Link](typeof(List<Inventory>), "sampleVar");
public static Expression GetMyExpression1()
{
{
/** Copy ONLY the Query Expresion into the specified comment area
and return the expression **/
Expression assignExpr =
[Link](variableExpr_SortByProductPrice,
[Link]([Link](i=> [Link]).ToList()));
return assignExpr;
}
}
}
}

[Link]

//This is for your reference. Do not change the given code template
using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace LINQ2
{
public class Inventory
{
public String ProductId { set; get; }
public String ProductName { set; get; }
public int Quantity { set; get; }
public double ProductPrice { set; get; }
}
}

___________________________________________________________________________________
__

Entity framework
___________________

1. Sudent details

[Link]

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace StudentManagement //DO NOT Change the namespace name


{
public class Program //DO NOT Change the class name
{
public static CollegeContext ss = new CollegeContext();
public static void Main(string[] args)
{
//Implement the code here
Student sk = new Student();
[Link]("Enter Student Id");
[Link] = [Link]([Link]());
[Link]("Enter Student Name");
[Link] = [Link]();
[Link]("Enter Department");
[Link] = [Link]();
[Link]("Enter Enrollment Date");
[Link] = [Link]([Link]());
[Link]("Enter the phoneNumeber");
[Link] = [Link]([Link]());
Program pp = new Program();
[Link](sk);
}

public void AddStudent(Student student){ //Do not change the method


signature

//Add the student details to database.


[Link](student);
[Link]();
[Link]("Details Added Successfully");
}
}
}

[Link]

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace StudentManagement //DO NOT Change the namespace name


{
public class Student //DO NOT Change the class name
{
//Create the
public int StudentId {get; set;}
public string StudentName {get; set;}
public DateTime EnrolledDate{get; set;}
public string Department{get; set;}
public long PhoneNumber{get; set;}
}
}

[Link]

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace StudentManagement //DO NOT Change the namespace name


{
public class CollegeContext: DbContext //DO NOT Change the class name
{
//DO NOT Change the Context name 'StudentConnectionString'
public CollegeContext() : base("name=StudentConnectionString"){}

//Declare 'Students' Property of type Dbset with neccessary declaration


code.

public DbSet<Student> Students {set; get;}

}
}
_______________________________________________________

2. Book entry

[Link]

//THIS IS FOR REFERENCE ONLY. YOU ARE NOT REQUIRED TO MAKE ANY CHANGES HERE
using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Exercise1 //DO NOT Change the namespace name


{
public class Book //DO NOT Change the class name
{
public int BookId { get; set; }
public String BookName { get; set; }
public String BookAuthor { get; set; }
public String BookGenre { get; set; }
public double BookPrice { get; set; }

}
}

[Link]

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Exercise1 //DO NOT change the namespace name


{
public class BookUtil //DO NOT change the class name
{
LibraryContext cc = new LibraryContext();
public Book AddBook(Book book) //DO NOT change the method Name and
Signature
{
//Implement code to insert the book entity to database
[Link](book);
[Link]();
Book bb = new Book();
[Link]("Details Added Successfully");
return book;
}

public List<Book> GetBookByGenre(String Genre) //DO NOT change the method


Name and Signature
{
//Implement code to get the book entity from database based on Genre
List<Book> res = [Link]();
List<Book> ck = [Link](a=>[Link]==Genre).ToList();
return ck;
}
public List<Book> GetBooksList() //DO NOT change the method Name and
Signature
{
//Implement code to get the book list from database
List<Book> cs = [Link]();
return cs;

}
public Book UpdateBookPrice(int NewPrice,int Bookid) //DO NOT change the
method Name and Signature
{
//Implement code to update the book entity
Book temp = null;
temp = [Link](a=>[Link] == Bookid).FirstOrDefault();

[Link] = NewPrice;
[Link]();
return temp;

public Book DeleteBook(int BookId) //DO NOT change the method Name and
Signature
{
//Implement code to delete the book entity by Id

Book cl = [Link](a=> [Link] == BookId).First();


[Link](cl);
[Link]();
return cl;

}
}

[Link]

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace Exercise1 //DO NOT change the namespace name


{
public class LibraryContext:DbContext //DO NOT change the class name
{

//DO NOT change the context name


public LibraryContext() : base("name=BookStore")
{

}
//Implement Books of type DbSet
public DbSet <Book> Books{get; set;}
protected override void OnModelCreating(DbModelBuilder
modelBuilder)
{
//Implement code to make 'Book_id' required in entity 'Book' and table
name as mentioned in description

[Link]<Book>().Property(mc=>[Link]).HasColumnName("Book");
[Link]<Book>().ToTable("BookDetails");

}
}
}

[Link]

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Exercise1 //DO NOT change the namespace name


{
public class Program //DO NOT change the class name
{
static void Main(string[] args) //DO NOT change the 'Main' method
signature
{
//Implement code here
Book bb = new Book();
BookUtil bu = new BookUtil();
[Link]("Enter the Book Name");
[Link] = [Link]();
[Link]("Enter Book Author");
[Link] = [Link]();
[Link]("Enter Book Genre");
[Link] = [Link]();
[Link]("Enter Book Price");
[Link] = [Link]();
[Link] = [Link]([Link]());

var ccc = [Link](bb);


[Link]("Enter the Book Name");
[Link] = [Link]();
[Link]("Enter Book Author");
[Link] = [Link]();
[Link]("Enter Book Genre");
[Link] = [Link]();
[Link]("Enter Book Price");
[Link] = [Link]();
[Link] = [Link]([Link]());

ccc = [Link](bb);

[Link]("Enter the Genre");


string gk = [Link]();
var ddd = [Link](gk);
var lll = [Link]();
}
}
}

______________________________________________________________________________

[Link] repository

[Link]

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Exercise1 //DO NOT Change the namespace name


{
public class Program //DO NOT Change the class name
{
static void Main(string[] args)
{
//Implement code here
CourseContext cl = new CourseContext();
Course ab = new Course();

CourseRepository repository = new CourseRepository(cl);

[Link]("Enter Course Id");


[Link] = Convert.ToInt32([Link]());

[Link]("Enter Course Name");


[Link] = [Link]();

[Link]("Enter Duration");
[Link] = Convert.ToInt32([Link]());

[Link]("Enter Course Fee");


[Link] = [Link]([Link]());

[Link]("Enter Instructor Name");


[Link] = [Link]();

[Link](ab);
[Link]("Details Added Successfully.");

[Link]("\nGet Course Details By id");


int searchId = Convert.ToInt32([Link]());
var foundCourse = [Link](searchId);
if(foundCourse != null){
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
}
[Link]("\nGet Course List");
var courseList = [Link]();
foreach(var c in courseList){
[Link]($"{[Link]}-{[Link]}-{[Link]}-
{[Link]}-{[Link]}");

}
[Link]("\nUpdate Course Fee");
int updateId = Convert.ToInt32([Link]());
double fee = [Link]([Link]());
var updatedCourse = [Link](updateId, fee);
if(updatedCourse != null){
[Link]("Updated Successfully");
}
else{
[Link]($"Course with ID {updateId} not found.");
}
}
}
}

[Link]

//THIS CLASS IS FOR REFERENCE. YOU NEED NOT CHANGE


using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Exercise1 //DO NOT Change the namespace name


{
public class Course //DO NOT Change the class name
{

public int CourseId { get; set; }


public String CourseName { get; set; }
public double CourseFee { get; set; }
public int Duration { get; set; }
public String InstructorName { get; set; }
}
}

[Link]

using System;
using [Link];
using [Link];
using [Link];

namespace Exercise1 //DO NOT Change the namespace name


{
public class CourseRepository //DO NOT Change the class name
{
//DO NOT Change the variable or method signature. Add only the required
code inside the method.
private CourseContext context;

public CourseRepository(CourseContext context)


{
//Implement code here
[Link] = context;
}

public IList<Course> GetCourseList()


{
//Implement code here

return [Link]();
}

public Course GetCourseByID(int courseId)


{
//Implement code here
return [Link](a=>[Link] ==
courseId).FirstOrDefault();
}

public void InsertCourse(Course course)


{
//Implement code here
[Link](course);
[Link]();
}

public Course UpdateCourseFee(int id, double fee)


{
//Implement code here
var course = [Link](id);
if(course == null){
return null;
}
[Link] = fee;
[Link]();
return course;
}
}
}

course [Link]

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Exercise1 //DO NOT Change the namespace name


{
public class CourseContext:DbContext //DO NOT Change the class name
{

//DO NOT Change the Context name 'CourseConnectionString'


public CourseContext() : base("name=CourseConnectionString"){}
public virtual DbSet<Course> Courses {get; set;}
//Declare 'Courses' of type Dbset and add neccessary declaration code.

}
}

___________________________________________________________________________________
________________-

You might also like