I am trying to understand left outer joins in LINQ to Entity. For example I have the following 3 tables:
Company, CompanyProduct, Product
The CompanyProduct is linked to its two parent tables, Company and Product.
I want to return all of the Company records and the associated CompanyProduct whether the CompanyProduct exists or not for a given product. In Transact SQL I would go from the Company table using left outer joins as follows:
SELECT * FROM Company AS C
LEFT OUTER JOIN CompanyProduct AS CP ON C.CompanyID=CP.CompanyID
LEFT OUTER JOIN Product AS P ON CP.ProductID=P.ProductID
WHERE P.ProductID = 14 OR P.ProductID IS NULL
My database has 3 companies, and 2 CompanyProduct records assocaited with the ProductID of 14. So the results from the SQL query are the expected 3 rows, 2 of which are connected to a CompanyProduct and Product and 1 which simply has the Company table and nulls in the CompanyProduct and Product tables.
So how do you write the same kind of join in LINQ to Entity to acheive a similiar result?
I have tried a few different things but can't get the syntax correct.
Thanks.
Solved it!
Final Output:
theCompany.id: 1
theProduct.id: 14
theCompany.id: 2
theProduct.id: 14
theCompany.id: 3
Here is the Scenario
1 - The Database
--Company Table
CREATE TABLE [theCompany](
[id] [int] IDENTITY(1,1) NOT NULL,
[value] [nvarchar](50) NULL,
CONSTRAINT [PK_theCompany] PRIMARY KEY CLUSTERED
( [id] ASC ) WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY];
GO
--Products Table
CREATE TABLE [theProduct](
[id] [int] IDENTITY(1,1) NOT NULL,
[value] [nvarchar](50) NULL,
CONSTRAINT [PK_theProduct] PRIMARY KEY CLUSTERED
( [id] ASC
) WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY];
GO
--CompanyProduct Table
CREATE TABLE [dbo].[CompanyProduct](
[fk_company] [int] NOT NULL,
[fk_product] [int] NOT NULL
) ON [PRIMARY];
GO
ALTER TABLE [CompanyProduct] WITH CHECK ADD CONSTRAINT
[FK_CompanyProduct_theCompany] FOREIGN KEY([fk_company])
REFERENCES [theCompany] ([id]);
GO
ALTER TABLE [dbo].[CompanyProduct] CHECK CONSTRAINT
[FK_CompanyProduct_theCompany];
GO
ALTER TABLE [CompanyProduct] WITH CHECK ADD CONSTRAINT
[FK_CompanyProduct_theProduct] FOREIGN KEY([fk_product])
REFERENCES [dbo].[theProduct] ([id]);
GO
ALTER TABLE [dbo].[CompanyProduct] CHECK CONSTRAINT
[FK_CompanyProduct_theProduct];
2 - The Data
SELECT [id] ,[value] FROM theCompany
id value
----------- --------------------------------------------------
1 company1
2 company2
3 company3
SELECT [id] ,[value] FROM theProduct
id value
----------- --------------------------------------------------
14 Product 1
SELECT [fk_company],[fk_product] FROM CompanyProduct;
fk_company fk_product
----------- -----------
1 14
2 14
3 - The Entity in VS.NET 2008
alt text http://i478.photobucket.com/albums/rr148/KyleLanser/companyproduct.png
The Entity Container Name is 'testEntities' (as seen in model Properties window)
4 - The Code (FINALLY!)
testEntities entity = new testEntities();
var theResultSet = from c in entity.theCompany
select new { company_id = c.id, product_id = c.theProduct.Select(e=>e) };
foreach(var oneCompany in theResultSet)
{
Debug.WriteLine("theCompany.id: " + oneCompany.company_id);
foreach(var allProducts in oneCompany.product_id)
{
Debug.WriteLine("theProduct.id: " + allProducts.id);
}
}
5 - The Final Output
theCompany.id: 1
theProduct.id: 14
theCompany.id: 2
theProduct.id: 14
theCompany.id: 3
IT should be something like this....
var query = from t1 in db.table1
join t2 in db.table2
on t1.Field1 equals t2.field1 into T1andT2
from t2Join in T1andT2.DefaultIfEmpty()
join t3 in db.table3
on t2Join.Field2 equals t3.Field3 into T2andT3
from t3Join in T2andT3.DefaultIfEmpty()
where t1.someField = "Some value"
select
{
t2Join.FieldXXX
t3Join.FieldYYY
};
This is how I did....
You'll want to use the Entity Framework to set up a many-to-many mapping from Company to Product. This will use the CompanyProduct table, but will make it unnecessary to have a CompanyProduct entity set in your entity model. Once you've done that, the query will be very simple, and it will depend on personal preference and how you want to represent the data. For example, if you just want all the companies who have a given product, you could say:
var query = from p in Database.ProductSet
where p.ProductId == 14
from c in p.Companies
select c;
or
var query = Database.CompanySet
.Where(c => c.Products.Any(p => p.ProductId == 14));
Your SQL query returns the product information along with the companies. If that's what you're going for, you might try:
var query = from p in Database.ProductSet
where p.ProductId == 14
select new
{
Product = p,
Companies = p.Companies
};
Please use the "Add Comment" button if you would like to provide more information, rather than creating another answer.
LEFT OUTER JOINs are done by using the GroupJoin in Entity Framework:
http://msdn.microsoft.com/en-us/library/bb896266.aspx
The normal group join represents a left outer join. Try this:
var list = from a in _datasource.table1
join b in _datasource.table2
on a.id equals b.table1.id
into ab
where ab.Count()==0
select new { table1 = a,
table2Count = ab.Count() };
That example gives you all records from table1 which don't have a reference to table2.
If you omit the where sentence, you get all records of table1.
Please try something like this:
from s in db.Employees
join e in db.Employees on s.ReportsTo equals e.EmployeeId
join er in EmployeeRoles on s.EmployeeId equals er.EmployeeId
join r in Roles on er.RoleId equals r.RoleId
where e.EmployeeId == employeeId &&
er.Status == (int)DocumentStatus.Draft
select s;
Cheers!
What about this one (you do have a many-to-many relationship between Company and Product in your Entity Designer, don't you?):
from s in db.Employees
where s.Product == null || s.Product.ProductID == 14
select s;
Entity Framework should be able to figure out the type of join to use.
Related
Here is a jql query that I got the result of
assignee in membersOf("project")
This would return the issues of members belonging to project.
I would like to know in which table of jira database would this data(or this link of which member belong to which proj) is stored?
Group Membership
The group memberships are stored in the CWD_MEMBERSHIP table.
Example:
SELECT LOWER_CHILD_NAME
FROM CWD_MEMBERSHIP
WHERE MEMBERSHIP_TYPE = 'GROUP_USER'
AND LOWER_PARENT_NAME = 'jira-administrators';
Example2, to fetch the user infos as well:
SELECT
U.*
FROM
CWD_MEMBERSHIP M
INNER JOIN CWD_USER U
ON
M.LOWER_CHILD_NAME = U.LOWER_USER_NAME
WHERE
M.MEMBERSHIP_TYPE = 'GROUP_USER' AND
M.LOWER_PARENT_NAME = 'jira-administrators';
Project Role Membership
The project role memberships however are in the PROJECTROLE and PROJECTROLEACTOR tables.
Example:
SELECT A.ROLETYPEPARAMETER AS USERNAME, R.NAME AS ROLENAME, P.PKEY || ' - ' || P.PNAME AS PROJECTNAME
FROM PROJECTROLEACTOR A
INNER JOIN PROJECTROLE R ON A.PROJECTROLEID = R.ID
INNER JOIN PROJECT P ON A.PID = P.ID
WHERE P.PKEY = 'YOUR_PKEY_COMES_HERE'
ORDER BY 3, 1, 2;
Example2, to get users that are explicitly assigned to project roles (not through groups):
SELECT A.ROLETYPEPARAMETER AS USERNAME, R.NAME AS ROLENAME, P.PKEY || ' - ' || P.PNAME AS PROJECTNAME
FROM PROJECTROLEACTOR A
INNER JOIN PROJECTROLE R ON A.PROJECTROLEID = R.ID
INNER JOIN PROJECT P ON A.PID = P.ID
INNER JOIN CWD_USER U ON LOWER(A.ROLETYPEPARAMETER) = U.LOWER_USER_NAME
ORDER BY 3, 1, 2;
Issue Change History
To get the issue history, you'll need the changegroup and changeitem tables joined to jiraissue. Changegroup stores who changed and when, changeitem contains the olda and new data, alongside what field was changed.
Example of listing ex-assignees:
SELECT
CG.AUTHOR AS CHANGE_USER ,
CG.CREATED AS CHANGE_WHEN ,
CI.FIELD AS CHANGED_WHAT,
CI.OLDVALUE AS CHANGED_FROM,
CI.NEWVALUE AS CHANGED_TO
FROM
JIRAISSUE JI
INNER JOIN CHANGEGROUP CG
ON
JI.ID = CG.ISSUEID
INNER JOIN CHANGEITEM CI
ON
CG.ID = CI.GROUPID
WHERE
JI.PROJECT = 10100 AND
JI.ISSUENUM = 1234 AND
CI.FIELDTYPE = 'jira' AND
CI.FIELD = 'assignee'
ORDER BY
CG.CREATED ASC;
The last row's (newest created) newvalue must match jiraissue.assignee-s value.
I have two tables table1 and table2. Each table contains a column with itemPrice. I need to add the two columns together.
The SQL query below returns the correct SUM.
SELECT SUM(item1+ item2) FROM
(select SUM(t1.itemPrice) item1 from table1 t1 WHERE t1.userid=='jonh') tableA
CROSS JOIN
(select SUM(t2.itemPrice) item2 from table2 t2 WHERE t1.userid=='jonh') tableB
I am not been lazy but the above query has so many SUM functions that I don't know where to start to write LINQ queries.
Can anyone help?
Ceci,
Hopefully this will give you what you want...
from f in (
from x in ( from t1 in Table1
where t1.Userid.Equals("John")
select new { Userid = t1.Userid }
).Distinct()
select new { item1 = ( from z in Table1
where z.Userid.Equals("John")
select z.ItemPrice ).Sum() ??0 ,
item2 = ( from z in Table2
where z.Userid.Equals("John")
select z.ItemPrice ).Sum() ??0 }
) select new { total = f.item1 + f.item2 }
In the case where there are no records for "john" in one table, it will bring back a 0 and sum up the other tables.
hope this helps.
I'm having a bit of brain fade today and can't figure out how I should express this SQL query correctly using ActiveRecord/Squeel/ARel:
SELECT `d1`.* FROM `domain_names` d1
WHERE `d1`.`created_at` = (
SELECT MAX(`d2`.`created_at`)
FROM `domain_names` d2
WHERE `d2`.`owner_type` = `d1`.`owner_type`
AND `d2`.`owner_id` = `d1`.`owner_id`
AND `d2`.`key` = `d1`.`key`
)
Any ideas?
Background: The DomainName model has a polymorphic owner as well as a "key" field that allows owners to have many different types of domain name. The query above fetches the latest domain name for each unique [owner_type, owner_id, key] tuple.
Edit:
Here's the same query using JOIN:
SELECT `d1`.* FROM `domain_names` d1
JOIN (
SELECT `owner_type`, `owner_id`, `key`, MAX(`created_at`) max_created_at
FROM `domain_names`
GROUP BY `owner_type`, `owner_id`, `key`
) d2
ON `d2`.`owner_type` = `d1`.`owner_type`
AND `d2`.`owner_id` = `d1`.`owner_id`
AND `d2`.`key` = `d1`.`key`
WHERE `d1`.`created_at` = `d2`.`max_created_at`
I'm wondering if there is a possibility when you upload your highscore you can compare your score with the one of your friends (if simpler, only selected contacts)?
And if so, could someone point me in the right direction, how to do it? I did not find anything useful about this on google.
As far as I pressume it should be possible, because apps like WhatsApp also let you choose specific contacts you want to send a message.
Related to that: Can I just use a/the cloud for uploading highscore or should I use my webspace?
I am not answering this specific to iOS/etc.
What you would typically do is expose a REST (or POX/POJSON - plain old XML or plain old JSON) service on your website that your application communicates with - it would be responsible for negotiating friendships, uploading high scores and retrieving high scores. This would either hit a database under your control or it would connect to a cloud server; there is no problem with either approach (Azure is a good option if you want to apply my SQL concepts).
Inside your database you would maintain a list of friends - this is a very simple structure to set up. Essentially you want two tables that look like the following:
CREATE TABLE [UserAccount]
(
[ID] BIGINT IDENTITY(1,1) PRIMARY KEY,
[Name] NVARCHAR(255),
)
CREATE TABLE [Friendship]
(
[User1] BIGINT, -- Primary key, FK to [UserAccount].[ID]
[User2] BIGINT, -- Primary key, FK to [UserAccount].[ID]
)
This would allow you to indicate friendships along the lines of:
User: ID = 1, Name = Joe
User: ID = 2, Name = Fred
Friendship: User1 = 1, User2 = 2
Friendship: User1 = 2, User1 = 1
You can then query friends using the following query:
SELECT [F1].[User2] AS [ID] FROM [Friendship] AS [F1]
WHERE [F1].[User1] = #CurrentUser
-- Check for symmetric relationship.
AND EXISTS
( SELECT 1 FROM [Friendship] AS [F2]
WHERE [F2].[User2] = [F1].[User1] AND [F2].[User1] = [F1].[User2] );
You could make that a TVF (Table Value Function) if your SQL variant supports them. Next you would create a high score table and a table to map it to users.
CREATE TABLE [Highscore]
{
[ID] BIGINT IDENTITY(1,1) PRIMARY KEY,
[Score] INT,
}
CREATE TABLE [UserHighscore]
{
[UserID] BIGINT, -- Primary key, FK to User.ID
[HighscoreID] BIGINT, -- Primary key, FK to Highscore.ID
}
Some sample data for this would be:
-- In this game you can only score over 9000!
Highscore: ID = 1, Score = 9001
Highscore: ID = 2, Score = 9005
Highscore: ID = 3, Score = 9008
UserHighscore: UserID = 1, HighscoreID = 1
UserHighscore: UserID = 1, HighscoreID = 2
UserHighscore: UserID = 2, HighscoreID = 3
You can then query for your friends' highscores:
SELECT TOP(10) [U].[Name], [H].[Score] FROM [Friendship] AS [F1]
LEFT INNER JOIN [User] AS [U] ON [U].[ID] = [F1].[User2]
LEFT INNER JOIN [HighscoreUser] AS [HU] ON [HU].[UserID] = [F1].[User2]
LEFT INNER JOIN [Highscore] AS [H] ON [H].[ID] = [HU].[UserID]
WHERE [F1].[User1] = #CurrentUser
-- Check for symmetric relationship.
AND EXISTS
( SELECT 1 FROM [Friendship] AS [F2]
WHERE [F2].[User2] = [F1].[User1] AND [F2].[User1] = [F1].[User2] )
ORDER BY [H].[Score] DESC;
That query would give the top 10 score your friends; giving you their name and score.
I have a complex query that I'm trying to reproduce in LINQ to Entities, but I'm not there yet - is it possible?
The t-sql query looks like:
select distinct
C.id,
L.id
from
dp
join L on L.fk = DP.id
join M on ( M.l_code = L.l_code and M.dp_code = DP.dp_code )
join C on C.c_code = M.c_code
where not exists ( select id from map where map.cid = c.id and map.lid = l.id )
Tables look like:
DP:
id (pk)
dp_code (varchar)
L:
id (pk)
fk (fk to DP.ID)
l_code (varchar)
M:
id (pk)
l_code (varchar, matches value in L.l_code)
dp_code (varchar, matches value in DP.dp_code)
c_code (varchar, matches the value in C.c_code)
C:
id (pk)
c_code (varchar)
MAP:
id (pk)
cid (fk to C.id)
lid (fk to L.id)
My LINQ looks like:
IQueryable foo = from dp in ctx.DP
from l in dl.L
join m in ctx.M on l.l_code equals m.m_code
// Q1: how can I add: AND m.dp_code = dp.dp_code
join c in ctx.C on m.c_code = c.c_code
// this works, but why can't I do it as an AND in the join?
where m.dp_code == dp.dp_code
select new
{
cid = c.id,
cid = c.id
}.Distinct();
So, questions:
Q1: Why can't I do two conditions in the join? User error, or limitations in LINQ?
Q2: How can I add the NOT EXISTS to the query? I've looked at this question, but can't see how to implement the NOT EXISTS subquery.
You can, but it's usually wrong to do a join at all. Still, if you must, you use anonymous types: on new { l: l.l_code, d: dp.code } equals new { l: m_code, d: m.dp_code }
where !(from m in map where whatever select m).Any(). But as with (1), it's better to use associations.