I have two Restaurant and Location tables. Location table has foreign key LocationId stored in Restaurant table. I want to select city name from location table, while getting some information from Restaurant table too. How can I retrieve it using C#.
Thanks in advance.
var cityNameAndSomeRestaurantStuff = from r in Db.Restaurants
join l in Db.Locations
on r.LocationId equals l.LocationId
select new { l.CityName, r.SomeProperty };
Note that the result will be an IQueryable, so if you want it materialised, call .ToList() or some similar extension
Related
Let's suppose there is a database to manage students.
I'm using the following (simplified) classes:
Student
int id
string name
StudentEnrollmentInfo
int studentId
int courseId
Course
int courseInfo
string name
In the aforementioned tables, studentenrollmentinfo is a joining table, to handle the many to many relation between courses and students.
Now I'd like to query the names of all students who are currently enrolled in a list of different courses. For example:
Get all students enrolled in Biology, Maths and Arts, I'd like to get all students that are enrolled in ALL of those classes.
I have tried the following:
List<string> requiredCourses = new List<string>(){"Maths", "Biology", "Arts"};
var query = from student in _context.students
join enrollmentInfo in _context.StudentEnrollmentInfo on
student.id equals enrollmentInfo.studentId
join course in _context.Course on course.id equals enrollmentInfo.courseId
where requiredCourses.All(r => r.equals(course.name))
select(student.name)
This however does not work, my guess is because the joins, in addition to the joining table flattens the list, making it where every student with every course combination is a unique value, making it so that there is never any AND condition that is true.
Can anyone suggest something to point me in the right direction?
List requiredCourses = new List() { "Maths", "Biology", "Arts" };
var query = (from student in _context.students
join enrollmentInfo in _context.StudentEnrollmentInfo on student.id equals enrollmentInfo.studentId
join course in _context.Course on course.id equals enrollmentInfo.courseId
where **requiredCourses.Contains(course.name)**
select student.name).Distinct().ToList()
hope it will solve your purpose. distinct will help for your unique data
Please i have my two tables, i recently learnt to outer join the two in linq. My problem now is i want to use group by a particular column for a i want to return a sum as well. Below is how i join the tables.
var result = from l1 in repository.Locations
join l2 in repository.Rooms on l1.LocationName equals l2.Location
into l3
from tempLocations in l3.DefaultIfEmpty()
select new
{
l1.LocationName,
total = (tempLocations == null ? String.Empty : tempLocations.Location)
};
My problem is the column total, intend to group by l1.LocationName and the count() as total. Please any help would be appreciated.
UPDATE
I want to group by the Location Column and LocationName column from the l1 and l2 tables. I want to return something like.
The LocationNAme is from Locations Table , LOcation Column is from Rooms Table , i want to outer join the two tables where LocationNAme = Location and count existence of LocationName in Room Table .
I have an Items table with ItemID as primary key
I have a StoreItems table which has composite primary keys StoreID and ItemID. The ItemID uses a foreign key reference to the Items table.
I need to create a query which looks like this (goal is to select items which exist in two stores)
select * from Items i
inner join storeitems s1 on s1.ItemID = i.ItemID and s1.StoreID = myfirststoreid
inner join storeitems s2 on s2.ItemID = i.ItemID and s2.StoreID = mysecondstoreid
How can I do the fluent mapping to achieve this?
untested. it selects the ids first then gets the items. It might be possible to combine into one statement:
var itemsIdsInBothStores =
from i in session.Query<Item>()
from s in i.Stores
where s.Id == myfirststoreid || s.Id == mysecondstoreid
group i by i.Id into g
where g.Count() = 2
select g.Key;
var itemsInBothStores = ´session.QueryOver<Item>().WhereRestrictionOn(i => i.Id).In(itemsIdsInBothStores.ToList());
In such a two tables
table Person
{
int Id -> primary key
varchar name
varchar nick
int GroupId -> foreign key
}
table Group
{
int Id -> primary key
varchar name
}
If I use
var result = (from c in myDataBase.Group
select c).ToList<Group>();
I get only list of Group, but field System.Data.Objects.DataClasses.EntityCollection<Person> is empty. How should I change query to get also list of Persons?
It's not clear where System.Data.Objects.DataClasses.EntityCollection<Person> has to be, but I presume you are searching for Inner join
var query = from person in people
join group in groups on person.GroupId equals group .Id
select new {.. something... };
Here I presumed you have somewhere people (collection of Person types) and want to find all persons from that collection and their matching information from related Group.
If this is not what you're asking for, please clarify.
I solved problem with:
myDataBase.Group.Include("Person").Select(a => a).ToList();
Btw: what is equivalent of Include() in linq query from...where...select ?
I have a table called Students and a table called Majors, Students and Majors are joined by MajorId I have set this relationship already and have set the foreign key in the schema. When I access my Student object how can I return the MajorName column (this comes from the Majors table)? The only options I have in intellisense is Major_1, Major_1Reference, MajorId .
Major_1 should be a navigation property leading to the appropriate Major entry, so you should be able to access the Major's properties like this:
from s in ctx.Students
select s.Major_1.MajorName
You can use linq join statement like this to make query on the two tables...
var q = from s in Students
join m in Majors on s.MajorId equals m.MajorId
select new { m.MajorName };