Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to generate WHERE EXISTS sql command #81

Open
tb-mtg opened this issue Oct 24, 2024 · 0 comments
Open

How to generate WHERE EXISTS sql command #81

tb-mtg opened this issue Oct 24, 2024 · 0 comments

Comments

@tb-mtg
Copy link

tb-mtg commented Oct 24, 2024

The following code:

var users = from u in db.GetTable<AM112U>()
            from d in db.GetTable<AM114U>().Where(d => u.WINDOWSDOMAINID == d.ID)
            where d.NORMALIZEDNAME == "fakedomain"
            select u;

var query = from e in db.GetTable<AM113U>()
           where users.Where(u => e.AM_WINDOWSUSERID == u.ID).Any()
           select new { e.ID, e.HR_EMPLOYEEID, e.AM_WINDOWSUSERID };

Generates SQL:

SELECT	e.ID,	e.HR_EMPLOYEEID,	e.AM_WINDOWSUSERID
FROM	AM113U e
WHERE	(
		SELECT '.'
		FROM AM112U u
		INNER JOIN AM114U d ON u.WINDOWSDOMAINID = d.ID
		WHERE d.NORMALIZEDNAME = 'fakedomain' AND e.AM_WINDOWSUSERID = u.ID
		FETCH FIRST 1 ROWS ONLY
) IS NOT NULL

Which doesn't use WHERE EXISTS as expected and causes and error because of the FETCH FIRST 1 ROWS ONLY is unexpectedly added.

The expected SQL should be (which is what is generated when using SqlServer or Access DataProviders):

SELECT	e.ID,	e.HR_EMPLOYEEID,	e.AM_WINDOWSUSERID
FROM	AM113U e
WHERE EXISTS (
		SELECT '.'
		FROM AM112U u
		INNER JOIN AM114U d ON u.WINDOWSDOMAINID = d.ID
		WHERE d.NORMALIZEDNAME = 'fakedomain' AND e.AM_WINDOWSUSERID = u.ID
)

How can I use .Any() to produce a valid SQL statement using WHERE EXISTS?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant