I apologize for the non-working query. I created a test app, and I think the following will produce the result you desire:
int customerID = 1;
dbmlDataContext db = new dbmlDataContext();
var ctxApps = from ctxApp in db.AppNames
join kmdApp in db.Applications on ctxApp.ID equals kmdApp.AppNameID into j
from apps in j.DefaultIfEmpty()
where ctxApp.Applications.Count == 0 || apps.ID != customerID
orderby ctxApp.AppName1 ascending
select ctxApp;
foreach(AppName n in ctxApps)
{
System.Diagnostics.Debug.WriteLine(
"AppName: " + n.AppName1 + "; related apps: " + n.Applications.Count);
}
I created two tables as follows:
CREATE DATABASE tstdb;
go
use tstdb;
CREATE TABLE AppNames(ID int PRIMARY KEY, AppName varchar(50));
INSERT INTO AppNames
SELECT 1, "Excel"
UNION
SELECT 2, "Word"
UNION
SELECT 3, "Access"
UNION
SELECT 4, "ShadyApplication"
go
CREATE TABLE Applications(ID int PRIMARY KEY, AppNameID int REFERENCES AppNames(ID))
go
INSERT INTO Applications
SELECT 1, 1
UNION
SELECT 2, 1
UNION
SELECT 3, 2
UNION
SELECT 4, 3
go
If I run the Linq query without the where clause, I will get:
AppName: Access; related apps: 1
AppName: Excel; related apps: 2
AppName: Excel; related apps: 2
AppName: ShadyApplication; related apps: 0
AppName: Word; related apps: 1
With the where clause:
AppName: Access; related apps: 1
AppName: Excel; related apps: 2
AppName: ShadyApplication; related apps: 0
AppName: Word; related apps: 1
wisccal
Participant
762 Points
131 Posts
Re: LINQ to SQL. How to make this query?
Apr 28, 2008 05:26 AM|LINK
Hi,
I apologize for the non-working query. I created a test app, and I think the following will produce the result you desire:
int customerID = 1;dbmlDataContext db = new dbmlDataContext();
var ctxApps = from ctxApp in db.AppNames
join kmdApp in db.Applications on ctxApp.ID equals kmdApp.AppNameID into j
from apps in j.DefaultIfEmpty()
where ctxApp.Applications.Count == 0 || apps.ID != customerID
orderby ctxApp.AppName1 ascending
select ctxApp;
foreach(AppName n in ctxApps)
{
System.Diagnostics.Debug.WriteLine(
"AppName: " + n.AppName1 + "; related apps: " + n.Applications.Count);
}
I created two tables as follows:
CREATE DATABASE tstdb;
go
use tstdb;
CREATE TABLE AppNames(ID int PRIMARY KEY, AppName varchar(50));
INSERT INTO AppNames
SELECT 1, "Excel"
UNION
SELECT 2, "Word"
UNION
SELECT 3, "Access"
UNION
SELECT 4, "ShadyApplication"
go
CREATE TABLE Applications(ID int PRIMARY KEY, AppNameID int REFERENCES AppNames(ID))
go
INSERT INTO Applications
SELECT 1, 1
UNION
SELECT 2, 1
UNION
SELECT 3, 2
UNION
SELECT 4, 3
go
If I run the Linq query without the where clause, I will get:
AppName: Access; related apps: 1AppName: Excel; related apps: 2
AppName: Excel; related apps: 2
AppName: ShadyApplication; related apps: 0
AppName: Word; related apps: 1
With the where clause:
AppName: Access; related apps: 1
AppName: Excel; related apps: 2
AppName: ShadyApplication; related apps: 0
AppName: Word; related apps: 1
Is that the behavior you were after?
=============
Regards,
Steve
www.stkomp.com