Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to sql query convert to lambda expression.How to use lambda expression this query.

Class table:
Id,Name,Surname

School table:
Id,Name,SchoolInformation

Class Table:
1 Alex   John  
1 Alex   Michael
1 Alex   Martin
2 Mary   Kelvin

School Table:
1 Alex
2 Mary
3 Justin


What I have tried:

SQL
Select distinct c.id as id,c.Name from class as c school as s where c.id=s.id order by 1

How to convert to lambda expression this query.
I want to result:
1 Alex
2 Mary
Posted
Updated 7-Oct-19 0:16am
v3
Comments
Mohibur Rashid 5-Oct-19 19:23pm    
Do you know what is lambda expression?
How would you do if you do not use lambda expression?

You query is incorrect
Select distinct
t.id as id ------- WHERE DO YOU GET YOUR T?
, t.Name
from
class as c
school as s
where
c.id=s.id
order by 1
Member 14169626 6-Oct-19 2:38am    
sorry question is uptated c. lambda expression x=>x.id like to write the query.
But I don't know the distinct and combination of the two tables

Sorry, your question is unclear and/or your database is wrongly designed, because schoold.id and class.id can NOT be equal! A database model should looks like:
1 School => ∞ YearBooks
1 YearBook => ∞ Classes
1 Class => ∞ Students

So, you need at least 4 objects (tables):
1. Yearbooks table - describes a school year
2. Classes table - contains all classes, each entry describes a class for a particular school year
3. Students table - contains all registered students, each entry describes a particular student
4. StudentsInClass table - (many to many relationship between students and classes) contains ids of class and student

BTW: I'd recommend to replace old-fashioned SQL programming
SQL
Select distinct c.id as id,c.Name
from class as c, school as s
where c.id=s.id
order by 1

to new one (using Join's[^]):
SQL
SELECT DISTINCT c.id as id, c.Name
FROM class as c
    INNER JOIN school as s ON c.id=s.id
ORDER BY c.id


For further details, please see: Visual Representation of SQL Joins[^]

When you finish changing a database model and queries into proper ones, i'd recommend to to study this site: http://linq101.nilzorblog.com/linq101-lambda.php[^]
It might be helpfull in understang how to write Linq queries using lambda expressions.
 
Share this answer
 
I hope the following way helps you.

List<Student> _lstStudent = new List<Student>();
List<School> _lstSchool = new List<School>();
_lstSchool.Add(new School() { Id = 1, Name = "MVM" });
_lstSchool.Add(new School() { Id = 2, Name = "DPS" });
_lstSchool.Add(new School() { Id = 3, Name = "JHK" });

_lstStudent.Add(new Student() { Id = 1, SchoolID = 1, Name = "Rajat", Surname = "Jaiswal" });
_lstStudent.Add(new Student() { Id = 2, SchoolID = 1, Name = "Aayush", Surname = "Jaiswal" });
_lstStudent.Add(new Student() { Id = 3, SchoolID = 2, Name = "Aman", Surname = "Jaiswal" });
_lstStudent.Add(new Student() { Id = 4, SchoolID = 3, Name = "Bittu", Surname = "Jaiswal" });



var tmp = from aStudent in _lstStudent
          join aSchool in _lstSchool on aStudent.SchoolID equals aSchool.Id
          select new
          {
              aStudent.Id,
              aStudent.Name,
              aStudent.Surname,
              SchoolName = aSchool.Name
          };

var tmp1 = _lstStudent.Join(_lstSchool, aStudent => aStudent.SchoolID,
                                        aSchool => aSchool.Id,
                                        (aStudent, aSchool) => new
                                        {
                                            aStudent.Id,
                                            aStudent.Name,
                                            aStudent.Surname,
                                            SchoolName = aSchool.Name
                                        });

foreach (var a in tmp)
{
    Console.WriteLine("Student Name {0} and Surname  {1}  Study in {2} school",a.Name, a.Surname,a.SchoolName);

}

Console.ReadKey();
 
Share this answer
 

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