I am not sure if this can be done, but here's the scenario.
I want to turn this sql into linq:
SELECT * FROM Department d
INNER JOIN Employee e ON e.DepartmentID = d.DepartmentID
Department - Employee is 1 to many relationship.
I have created a custom object that I would like to populate the result into.
public class DepartmentSummary
{
public Department Department { get; set; }
public List<Employee> Employees {get; set;}
}
The Linq I came up with is
var result = from d in dba.Department
join e in dba.Employee d.DepartmentID equals e.DepartmentID into j1
select new DepartmentSummary
{
Department = d,
Employees = j1.ToList()
};
I tried it out and it's not working. Can anyone shed some light for me please? I would like to perform an inner join between Department and Employee. For each Department in the resultset, I would like to create one DepartmentSummary object which holds that department and a list of employees belonging to that department.
Does Linq provides an ad hoc solution for this or must I iterates through the result set and create a list of DepartmentSummary manually?
Thanks,
EDIT:
Looks like this works for me
var result = from d in dba.Department
join e in dba.Employee d.DepartmentID equals e.DepartmentID into j1
where j1.Count() > 0
select new DepartmentSummary
{
Department = d,
Employees = j1.ToList()
};
The thing is that you're not really taking one SQL and trying to create a Linq-query out of it.
If you were, you'd notice that your SQL query does not really produce one row per department, but it will repeat the department information for each employee in that department.
Now, an initial naive look would suggest you use a group-by clause, since that would allow you to split the data into individual groupings for each department, but groupings in SQL does not really give you a key+all-matching-rows type of result, rather it allows you to do aggregate calculations, like "for each department, how many employees do I have".
So, in order to do what you want, you need to basically do a normal join, which will give you each employee, coupled with the appropriate department information (ie. each employee will be linked to his/her department), and then you need to construct the rest of the data structure yourself.
Now, having said that, if you have the proper relationships set in your data context related classes, each department should already have some kind of property that contains all employees in that department, so perhaps the simple query is just "give me all departments", and then you can, for each department, retrieve the employees?
Of course, doing that would likely execute one SQL for each department, but in this case, you're back to "give me all employees with their department information" and you have to build code to handle the rest.
LINQ to SQL doesn't understand your ToList() call, but you might be able to select the sequence of joined elements and then use LINQ to Objects (via AsEnumerable()) to map to your DepartmentSummary object:
var qResult = from d in dba.Department
join e in dba.Employee d.DepartmentID equals e.DepartmentID into j1
select new
{
Department = d,
Employees = j1
};
var result = from d in qResult.AsEnumerable()
select new DepartmentSummary()
{
Department = d.Department,
Employees = e.Employees.ToList()
};
Sounds like you're looking to get around lazy loading?
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Department>(d => d.Employees);
using (var dba = new MyDataContext())
{
dba.LoadOptions = dlo;
var result = from d in dba.Department
select d;
}
Now, if you don't have a relationship defined between Department and Employees (the Linq2Sql designer will do this for you if you have database relationships setup) then you should look into doing that. It makes it all dramatically easier. In fact, you don't even need your campaign summary.
This problem is due to the nature of the query. When you join Department to Employee, you'll get back one record for every Employee. This means that your ToList() statement is expecting multiple employees per department, but due to the join, always getting one.
Change your query to
var result =
from d in dba.Department
select new tCampaignSummary
{
Department = d,
Employees = dba.Employee.Where(e => e.DepartmentID ==
d.DepartmentID).ToList()
};
I've tested this and it works.
What it does differently is selects only one record per Department (not per employee) then it gets the zero to many corresponding employees for each dept and converts them to a list.
Good luck!
EDIT
As requested, here is the generated SQL:
SELECT [t0].*, [t1].*
(
SELECT COUNT(*)
FROM [dbo].[Employee] AS [t2]
WHERE [t2].[DepartmentID] = [t0].[DepartmentID]
) AS [value]
FROM [dbo].[Department] AS [t0]
LEFT OUTER JOIN [dbo].[Employee] AS [t1]
ON [t1].[DepartmentID] = [t0].[DepartmentID]
ORDER BY [t0].[DepartmentID], [t1].[IndexID]
The only modification is that LINQ will not do [t0].*, instead it will enumerate each field. Since I had to guess at the fields, I left them out to make the SQL clearer.
Related
I am working with the AdventureWorks database and I am using the following query :
SELECT c.CustomerID, c.AccountNumber, soh.SalesOrderID, soh.SalesOrderNumber, sod.UnitPrice, sod.ProductID, p.Name
FROM Sales.SalesOrderHeader soh
inner join sales.SalesOrderDetail sod on soh.SalesOrderID = sod.SalesOrderID
inner join sales.Customer c on c.CustomerID = soh.CustomerID
inner join Production.Product p on p.ProductID = sod.ProductID
I have code already that populates the data like this just as it comes back:
Based on that I am wanting to merge the child list objects into a single parent to end up with a one to many structure if that makes sense.
As of right now I am hoping to use a lambda expression to do this. I have tried the following code but I don't want to have to specify --> o.CustomerID, o.AccountNumber in the group by because i would have to specify ALL the properties of my Customer class to have access to the final select as you can see in the code.
.GroupBy(o => new { o.CustomerID, o.AccountNumber }).Select(group => new Customer2
{
CustomerID = group.Key.CustomerID,
AccountNumber = group.Key.AccountNumber,
Orders = group.SelectMany(x => x.Orders).ToList()
}).ToList();
I would like the end result to look like this for each customer:
I hope this makes sense and thanks in advance for the help.
I have the following tables in my database:
SageAccount
ID (bigint)
LegacyID (nvarchar)
Customer (bit)
Consignments
ID (bigint)
Customer (nvarchar)
What I want to do is have a navigation property/association in my Linq to Sql dbml from Consignment to SageAccount. The difficulty with this is that not only do we need to match SageAccount.LegacyID => Consignments.Customer but we also need to only join to sage accounts where SageAccount.Customer is TRUE. So on the Consignments end, it isn't joining onto a field but instead a static value.
Is this possible in Linq to Sql? Note this database doesn't (and unfortunately can't) have any foreign keys setup in the database.
Yes it is possible. linq have join method. You can use it ike this in your situation:
var res = from sageAccount in _context.SageAccount
join consignments in _context.Consignments
on
new
{
LegacyID = sageAccount.LegacyID,
Customer = sageAccount.Customer
}
equals
new
{
LegacyID = consignments.ID,
Customer = true
}
select new { SageAccountID = sageAccount.ID };
Note that Property name, Type and order in the anonymous objects that you're joining on must match.
You can't use OR and AND in joins - use just equals one object to other.
This will have a this kind of result in your SQL:
SELECT [t0].[ID] AS [SageAccountID]
FROM [dbo].[SageAccount] AS [t0]
INNER JOIN [dbo].[Consignments] AS [t1] ON (([t0].[LegacyID]) = [t1].[ID])
AND ([t0].[Customer] = 1)
I have a LINQ query that will sort through lists of company employees and attendees in company meetings. I want to extract employees who are not attendees in meetings. Thus, a left outer join seems to be the best LINQ strategy. When I debugged and stepped through the code, employeesNotInMeetings returned all employees, failing to remove the employees who were meeting attendees. Why does this left outer join fail to remove the proper list entries?
//Query for all attendees who are employees
List<Attendee> employeesWhoAreAttendees = db.Attendees.Select(ea => ea).ToList();
//Query for all employees in database
List<Employee> employees = db.Employees.Select(ee => ee).ToList();
var employeesNotInMeetings = from emp in employees
join att in employeesWhoAreAttendees
on emp.EmployeeID equals att.EmployeeID into gj
from gji in gj.DefaultIfEmpty()
//If EmployeeID < 0, the attendee is not an employee
where emp.EmployeeID > 0
select emp;
Try something like this:
employees.Where(x => employeesWhoAreAttendees.All(y => x.EmployeedID != y.EmployeeID))
I'm thinking you probably want something like:
db.Employees.Where(e => !e.Attendees.Any());
Note that this is assuming you have a foreign key set up between the employee and attendee tables, and that the ICollection navigation property on Employee is named Attendees
Also please note, you generally shouldn't call ToList() directly on an unfiltered DBSet. It will pull back the entire table into memory, then do the filtering. You are going to get better performance if you let SQL do the filtering for you.
That is what a left join is supposed to do: it returns all entries on the left side (employees) even if there is no result on the right side (employeesWhoAreAttendees). If you want to get employees not in meetings - then you should use left excluding join and find those entries in the table employees where there is no entry (null) in the table employeesWhoAreAttendees
I annoys me very much when programmers use var instead being precise and write the correct type. In Microsoft tutorial: http://msdn.microsoft.com/en-us/library/bb397927.aspx
I have found this explanation of inner join(below). What is probably the type of innerJoinQuery because it looks like mash up of 2 strings but we don't know what is the result type.
Quote:
Join operations create associations between sequences that are not explicitly modeled in the data sources. For example you can perform a join to find all the customers and distributors who have the same location. In LINQ the join clause always works against object collections instead of database tables directly.
C#
var innerJoinQuery =
from cust in customers
join dist in distributors on cust.City equals dist.City
select new { CustomerName = cust.Name, DistributorName = dist.Name };
In LINQ you do not have to use join as often as you do in SQL because foreign keys in LINQ are represented in the object model as properties that hold a collection of items. For example, a Customer object contains a collection of Order objects. Rather than performing a join, you access the orders by using dot notation:
EDIT: Let's rephrase this question. What else I can put instead of var here?
The result of this linq query is a sequence of objects of an anonymous type with two properties. One of them is called CustomerName and the other is called DistributorName. Both of them are quite possible of type string.
In order you avoid the var, you have to declare a class with these two properties, like below:
class ClassName
{
public string CustomerName { get; set; }
public string DistributorName { get; set; }
}
Then you have to change you linq query to the following one:
IEnumerable<ClassName> innerJoinQuery = from cust in customers
join dist in distributors
on cust.City equals dist.City
select new ClassName
{
CustomerName = cust.Name,
DistributorName = dist.Name
};
However I don't see any benefit in the above approach if you jsut want to get the results of this query and iterate through them or something else. In other words, I think that var is used correctly here. I mean it doesn't createa any ambiguities of what you do.
How do I update two tables at the same time using Linq-to-SQL?
var z = from a in db.Products
join b in db.ProductSubcategories on
a.ProductSubcategoryID equals b.ProductSubcategoryID
join d in db.ProductCategories on
b.ProductCategoryID equals d.ProductCategoryID
select new { ProductName = a.Name, ProductCategory = d.Name,
ProductSubCategory = b.Name, Cost = a.StandardCost,
discontinuedDate = a.DiscontinuedDate,
ProductId=a.ProductID };
You have to update individual records from each table and then execute db.SubmitChanges();
In your query the output is an anonymous type, not a table type connected to the db context.
If you think in terms of SQL, linq2sql works pretty much the same. You can select a record set with a join, but you cannot update directly on this. You need to break it up and modify entries directly on Products, ProductCategories and ProductSubCategories, which equals the tables in your database.
If you want to modify a Product in Products then you have to modify the properties of that type, and not the anonymous type (joined type).