In this tutorial I am discussing some linq queries with answer. In this artical i will discuss linq queries based on sql, object and xml.
Following is the table schema which i am using for linq to sql basic queries:-
[Table]
public class Employee
{
[Column(Name ="emp_id",IsPrimaryKey =true,IsDbGenerated =true)]
public int ID { get; set; }
[Column(Name = "emp_name")]
public string name { get; set; }
[Column(Name = "emp_age")]
public int? age { get; set; }
[Column(Name = "emp_email")]
public string emailid { get; set; }
[Column(Name = "emp_gender")]
public char gender { get; set; }
[Column(Name = "emp_salary")]
public Int64? salary { get; set; }
}
Create the object of DataContext class
DataContext dc = new DataContext("Data Source =.; Initial Catalog = myfirstdatabase; Integrated Security = True");
var q1 = dc.GetTable<Employee>().Where(x => x.age > 25);
var q2 = dc.GetTable<Employee>.Where(x => x.gender == 'F');
var q3 = dc.GetTable<Employee>().Where(x => x.age > 25 & & x.age < 30);
var q4 = dc.GetTable<Employee>().Where(x => x.ID >= 103 && x.ID <= 106 && x.gender == 'F');
var q5 = dc.GetTable<Employee>().Where(x => x.name == "isha malhotra");
var q6 = dc.GetTable<Employee>().Where(x => x.age == 30);
var q7 = dc.GetTable<Employee>().Where(x => x.name.StartsWith("r"));
var q8 = dc.GetTable<Employee>().Where(x => x.emailid.Contains("gmail"));
var q9 = dc.GetTable<Employee>().Where(x => x.name.EndsWith("arora"));
var q10 = dc.GetTable<Employee>().Where(x => x.name.Contains("sh"));
var q11 = dc.GetTable<Employee>().Where(x => x.age == 20 || x.age == 30 || x.age == 35);