Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a small LINQ problem make me crazy
I am new in the field of programming "Small Programmer".
Plz I need fast ,simple answer using my Style in coding.
My problem Is
Suppose if I have the this database
City Table :  City_ID As PK ,City_Name .
Department Table : City_ID As FK ,Dept_ID As PK ,Dept_Name .
Employee Table :Dept_ID FK ,Emp_ID As PK ,Emp_Name .
1- If I have City Name In string C;
I want To retreve All Assosiated Employee Names That Works In Depaartments Placed In City that have City Name That In string C ;
2- If I have Employee Name In string E;
I want To retreve City Name where That Employee Placed In ;
my Style in coding Is:
-----------------------------------------------------------------------------------
Using Datacontext Like My simple Example here to Retreve Department name that have Employee Name In string E ;

C#
public partial class _Default : System.Web.UI.Page
{
    ProDataContext M = new ProDataContext();//Object for Database
    protected void Page_Load(object sender, EventArgs e)
    {
        string E;
        string D = M.Departments.Where(x => x.Dep_ID == M.Employees.Where(n => n.Emp_Name == E).Single().Dep_ID).Single().Dep_Name;
     }

}

------------------------------------------------------------------------------------

Thanx.....


[edit]Code blocks sorted out - OriginalGriff[/edit]
Posted
Updated 23-Jul-11 21:49pm
v2
Comments
Mycroft Holmes 24-Jul-11 18:54pm    
So not only do you want the codz but you want it in your style of code. Read the guidelines, asking for the codz is frowned upon. Do a search for 101 Linq examples, it has been immensely useful to me.

1 solution

C#
// Solution Number 1:  (Get Employees)
DataClasses3DataContext db = new DataClasses3DataContext();
            var tbl = from p in db.Employees
                      where p.Department.City.City_Name == txtCityName.Text
                      select p;


// Solution Number 2: (Get City Name)
DataClasses3DataContext db = new DataClasses3DataContext();
            var tbl = (from p in db.Employees
                       where
                          p.Emp_Name == txtEmployeName.Text
                       select p).Select(p => p.Department.City);
            dataGridView1.DataSource = tbl;


Wish this helps you
Hamid Noahdi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900