I am trying to select everything from a table and then make a join with two other tables, from which I need just some of the columns. The final list will be populated into a DevExpress GridControl. The main table that I take the data from has these columns:
Id | ScheduleId | AccountId | Description
I can easily select everything from this table and return. Here is my list:
public IList<StatusDescription> getStatusDescription()
{
var listOfItems = from e in context.StatusDescription select e;
return listOfItems.ToList<StatusDescription>();
}
The other two tables are Schedules and Accounts, they look as follows:
Id | ScheduleName | DateFrom | DateTo
and
Id | AccountName | AccountCode
I would like to join the DateFrom and DateTo from the Schedule table and the AccountCode from the Account table. Is it possible to get all that into a single list. I can easily bind it later to the GridControl
var listOfItems = from e in context.StatusDescription
join s in context.Schedules on e.ScheduleId equals s.Id
join a in context.Accounts on e.AccountId equals a.Id
select new
{
Description = e.Description,
ScheduleStart = s.DateFrom,
ScheduleEnd = s.DateTo,
AccountCode = a.AccountCode
}
var Query = from p in context.StatusDescription
select new
{
p.Id,
p.Description,
p.Schedule.DateFrom,
p.Schedule.DateTo,
p.Account.AccountCode
};
hope it helps
Related
Have not come across an exact use-case for what I'm trying to achieve on Stackoverflow so will explain it in the hopes someone can assist.
I have two tables, one contains a master activity list and the other contains who completed those activities.
Table A is the Activity Table. This is a distinct list of things that can be done.
ID | Activity
---------------------------
1 | Change Oil
2 | Change Airfilter
3 | Change Brake Fluid
Table B is the Activity Log table. This tracks where people have done one of the above Activities. ActivityID links to ID on Table A.
ID | ActivityID | CompletedBy
---------------------------------------
1 | 1 | john#auto.com
2 | 1 | sally#auto.com
3 | 3 | john#auto.com
What I am trying to produce is a list of all activities, but then only for a distinct person. I have tried multiple ways for this, but can only get the query to show where values exist in both tables.
My preferred output would be the following, where in the query i have asked to show me the full Activity list and also to show where John has completed anything. If there is no record in Table B for this activity, to show a blank value.
ID | Activity | CompletedBy
-------------------------------------------
1 | Change Oil | john#auto.com
2 | Change Airfilter |
3 | Change Brake Fluid | john#auto.com
Here is my current SQL Query in my attempt to work this one out, which right now the results only return ID 1 and 3 from that example above, where john actually has a record in Table B.
Select a.ID, a.Activity, b.CompletedBy
FROM ActivityList a
LEFT OUTER JOIN ActivityLog b
ON a.ID = b.ActivityID
Where CompletedBy = 'john#auto.com'
GROUP BY a.ID, a.Activity, b.CompletedBy
Any help or pointers in the right direction would be greatly appreciated.
If you move the CompletedBy restriction into the join conditions you would not need the group by.
Select a.ID, a.Activity, b.CompletedBy
FROM ActivityList a
LEFT OUTER JOIN ActivityLog b
ON a.ID = b.ActivityID and b.CompletedBy = 'john#auto.com'
Change your where clause to Where CompletedBy = 'john#auto.com' or CompletedBy is null so it matches on a text match or the case where the name does not exist at all. You can also use isnull( to convert the null value to a empty string if that is the behavior you are wanting.
Select a.ID, a.Activity, isnull(b.CompletedBy, '') as CompletedBy
FROM ActivityList a
LEFT OUTER JOIN ActivityLog b
ON a.ID = b.ActivityID
Where CompletedBy = 'john#auto.com' or b.CompletedBy is null
GROUP BY a.ID, a.Activity, b.CompletedBy
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable table_A = new DataTable();
table_A.Columns.Add("ID", typeof(int));
table_A.Columns.Add("Activity", typeof(string));
table_A.Rows.Add(new object[] { 1, "Change Oil"});
table_A.Rows.Add(new object[] { 2, "Change Airfilter"});
table_A.Rows.Add(new object[] { 3, "Change Brake Fluid"});
DataTable table_B = new DataTable();
table_B.Columns.Add("ID", typeof(int));
table_B.Columns.Add("ActivityID", typeof(string));
table_B.Columns.Add("CompletedBy", typeof(string));
table_B.Rows.Add(new object[] { 1, 1, "john#auto.com"});
table_B.Rows.Add(new object[] { 2, 1, "sally#auto.com"});
table_B.Rows.Add(new object[] { 3, 3, "john#auto.com"});
var groups = (from a in table_A.AsEnumerable()
join b in table_B.AsEnumerable() on a.Field<int>("ID") equals b.Field<int>("ID")
select new { a = a, b = b})
.GroupBy(x => x.b.Field<string>("CompletedBy"))
.ToList();
}
}
}
----------
Select a.ID, a.Activity, b.CompletedBy
FROM ActivityList a
Right OUTER JOIN ActivityLog b
ON a.ID = b.ActivityID
Where CompletedBy = 'john#auto.com'
GROUP BY a.ID, a.Activity, b.CompletedBy
Your query is correct just change the potion of condition.
Select a.ID, a.Activity, b.CompletedBy
FROM ActivityList a
LEFT JOIN ActivityLog b
ON a.ID = b.ActivityID AND b.CompletedBy = 'john#auto.com'
GROUP BY a.ID, a.Activity, b.CompletedBy
Using these 2 tables:
employee(id, name)
payroll(name, salary)
I need to modify & convert this SQL query to linq:
SELECT employee.id, employee.name, payroll.salary
FROM employee left OUTER JOIN payroll
ON employee.name like payroll.name + '%'
to get unmatched records from those 2 tables. Any idea?
You can try this query:
var query = from emp in context.employee
join res in (from emp in context.employee
from pay in context.payroll
where emp.name.StartsWith(pay.name)
select new
{
emp.id,
pay.salary
})
on emp.id equals res.id into subRes
from sub in subRes.DefaultIfEmpty()
select new
{
emp.id,
emp.name,
sub?.salary
};
Good morning SO.
I have 3 tables (Sql Server) with this layout:
GenreMovies: | RowID | MovieGenre|
GenreMusic: |RowID | MusicGenre |
GenrePodcast:| RowID | PodcastGenre|
I have a combobox/dropdown that I am trying to populate with a Query:
var infoQuery =
(from MovieGen in dbContext.GenreMovies
select MovieGen.RowID)
.Union
(from MusicGen in dbContext.GenreMusics
select MusicGen.RowID).Union(from PodcastGen in dbContext.GenrePodcasts select PodcastGen.RowID).ToList();
GridSortSearch.DataTextField = "MovieGenre";
GridSortSearch.DataSource = infoQuery;
GridSortSearch.DataBind();
I have two issues the query is only being populated with "RowID"
and the combobox/dropdown is failing at the "DataTextField"
Like I said you need to return the same rows on results to achieve what you want.
Something like this, notice the change to the selects and the name of DataTextField value.
var infoQuery =
(from MovieGen in dbContext.GenreMovies
select new { MovieGen.RowID, Genre=MovieGen.MovieGenre})
.Union
(from MusicGen in dbContext.GenreMusics
select new {MusicGen.RowID, Genre=MusicGen.MusicGenre).Union(from PodcastGen in dbContext.GenrePodcasts select new {PodcastGen.RowID, Genre=PodcastGen.PodcastGenre).ToList();
GridSortSearch.DataTextField = "Genre";
GridSortSearch.DataSource = infoQuery;
GridSortSearch.DataBind();
I couldn't compile/test this where I am at the moment but should work.
I have this table (Prefrences_Table)
--------------------------
|student | Preferences |
--------------------------
Stud A | Stud B
Stud A | Stud C
Stud B | Stud E
Stud B | Stud A
Stud C | Stud F
Stud F | Stud B
--------------------------
If "Stud A" has added "Stud B" in his Preferences list, i would like to check if "stud B" has also added "stud A" in his preference, so i can add both of them in one group.
How can this be done using SQL or C#?
A self-join should work just fine here. The additional predicate returns only the first instance of the match to avoid duplicates.
select t.student, t1.student
from
Prefrences_Table t
inner join Prefrences_Table t1
on t.student = t1.preferences
and t.preferences = t1.student
and t.student < t1.student
this might give you anwer to your question, field mutual will be one if both students added the other in preferences, zero otherwise
SELECT T1.student, T2.Preferences,
(SELECT COUNT(*) FROM Prefrences_Table T2 WHERE T2.Preferences = T1.student AND T2.student = T1.Preferences) AS mutual
FROM Prefrences_Table T1
Another alternative would be the following:
SELECT * FROM
(
SELECT PM.student, PM.Preferences,
(SELECT COUNT(student) FROM Prefrences_Table AS PI WHERE PI.Preferences = PM.student
AND PI.student = PM.Preferences) AS CheckCross
FROM Prefrences_Table AS PM
) AS PD
WHERE PD.CheckCross > 0
You have some SQL answers, here is one in c#/linq.
var list = new List<Prefrences_Table>();
var results = (from t in list
join t1 in list on t.student equals t1.preferences
where
t.student == t1.preferences &&
t.preferences == t1.student &&
string.CompareOrdinal(t.student, t1.student) < 0
select new {t.student, t1.student}
);
I have a stored procedure that is returning data in this format:
EmployeeID | DepartmentID
---------------------
1 | 1
2 | 1
3 | 2
4 | 4
5 | 4
I'm getting the results like so:
List<spResult> results = DataContext.sp().ToList();
I'd like to get a list of Employees for a certain Department, based on the data returned from the stored procedure. Something like:
int departmentId = 1;
List<Employee> employees = (from e in DataContext.Employees
//where...
select e).ToList();
How do I format my where clause to get the EmployeeIDs from the result set that have the given DepartmentID?
How about:
List<spResult> results = DataContext.sp().ToList();
int departmentId = 1;
var departmentEmployees = from de in results
where de.DepartmentId == departmentId
select de.EmployeeID;
List<Employee> employees = (from e in DataContext.Employees
where departmentEmployees.Contains(e.ID)
select e).ToList();
You could get a subset of keys:
var empKeys = results.Where(i => i.DepartmentID = departmentID);
And then use this list in the query like:
List<Employee> employees = (from e in DataContext.Employees
where empKeys.Contains(e.EmployeeID)
select h).ToList();
HTH.
You should also be able to do something like this:
List<Employee> employees = DataContext.Employees.Where(e => empKeys.Contains(e.EmployeeID)).ToList()