How to use 'In' SQL keyword in Entity Framework? - c#

This is my SQL command
SELECT KEY,NAME
from COMPANY c
WHERE KEY IN (select KEY from USER_COMPANY where UserId = #UserId)
order by NAME asc
So I want to convert it to Entity Framework.
I try like this
var userCompany = (from u in db.USER_COMPANY
where u.UserId == UserId
select(u.KEY));
var user = (from c in db.COMPANY
where (c => userCompany.Contains(c.KEY)
select c);
but it is not working.
How to use the SQL IN keyword in Entity Framework?

Try this:
var query = from c in db.COMPANY
where (from u in db.USER_COMPANY
where u.UserId == UserId
select u.KEY).Contains(c.KEY)
orderby c.NAME
select c.KEY, c.NAME;

Note that this SQL query has the exact same meaning:
SELECT c.KEY, c.NAME
FROM COMPANY c
JOIN (SELECT DISTINCT KEY FROM USER_COMPANY where UserId = #UserId) u
ON U.KEY = C.KEY
ORDER BY c.NAME asc
So you should be able to just do:
var userCompany = (from u in db.USER_COMPANY
where u.UserId == UserId
select(u.KEY)).Distinct();
var result = from c in db.COMPANY
join u in userCompany
on c.KEY = u.KEY
select new {c.KEY, c.NAME};

My understanding is that the translation from .Contains() to IN is only just being added to EF6.
According to this work item (http://entityframework.codeplex.com/workitem/245) and the release note: Improved performance of Enumerable.Contains in LINQ queries.
So look for it in EF6

Related

Writing a SQL query with LINQ

I have this so far:
var query = (from g in this.Context.Groups
where g.ID == groupID &&
g.TaxId == groupTaxId
select g.Group_K);
Then the SQL query I want to add is this:
select zipCode
from ADDRESSES
where ADDRESS_K in
(select ADDRESS_K
from GROUPADDRESS
where GROUP_K = "is in Group_K found above "
and address_type = "address type variable I pass into this method"
and ACTIVE = 1)
Notice that GROUPADDRESS is the bridge table between GROUPS and Addresses table that has Group_K and Address_K
I can't figure it out how to add a new LINQ query or update the one I have to account for the new SQL I am trying to add. Thanks for help.
Groups Table: Group_K, ID, TaxId
Addresses Table: Address_K, Zip
GroupAddress Table: Group_K, Address_K, Address_Type
Assuming you have a GroupAddress in your context:
var query = (from g in this.Context.Groups
join ga in this.Context.GroupAddress on g.AddressK equals ga.AddressK
where g.ID == groupID &&
g.TaxId == groupTaxId
select g.Group_K);
Edit:
Right so GroupAddress sits between Group and Address. Assuming you've set up relationships in your database, you will have navigation properties you can test against:
where g.Address == ''
EF drops link tables so your navigation is simpler. If not available in EF, add another join to the above.
var query = (from g in this.Context.Groups
join ga in this.Context.GroupAddress on g.Group_K equals ga.Group_K
join a in this.Context.GroupAddress on g.Address_K equals ga.Address_K
where g.ID == groupID &&
g.TaxId == groupTaxId
select g.Group_K);
Maybe something like this:
var groupID=1;
var groupTaxId=1;
var result=
(
from a in this.Context.ADDRESSES
where
(
from ga in this.Context.GROUPADDRESS
join g in this.Context.Groups
on g.GROUP_K equals ga.GROUP_K
where ga.ID == groupID
where g.TaxId== groupTaxId
where ga.ACTIVE == 1
select ga.ADDRESS_K
).Contains(a.ADDRESS_K)
select new
{
a.zipCode
}
).ToList();

How To Convert Sql Query to Linq Which contans Subquery

My Query as Follows
`Select * from daps_user_activity where Userid In (Select Userid from daps_portaluser where EMR_ID = 24855) `
What is the equivalent query in linq please help me...
Try this, it's better that you use a join in this instance, instead of a sub-query:
var results = (from a in daps_user_activity
join u in daps_portaluser on a.Userid equals u.Userid
where u.EMR_ID == 24855
select a).ToList()
Alternatively, you could use this:
var results = (from a in daps_user_activity
from u in daps_portaluser
where u.EMR_ID == 24855
&& a.Userid == u.Userid
select a).ToList()
To me, it shows more clearly the main query and the subquery.
Credit goes to #Bruno Brant at Convert SQL Query (with Correlated Subquery) to LINQ in C#

Linq's Column In (Select ...) Statement

How do you write a linq statement that does the equivalent of this subselect in MS SQL:
... WHERE
tblXref.Organization_Id IN (SELECT Organization_Id
FROM AppUser au INNER JOIN [User] u ON au.User_Id = u.Id
WHERE u.Username = usernameVariable)
Well, it's probably simpler to write the inner query separately (remembering that you're not executing the query):
var innerQuery = from au in db.AppUsers
join u in db.Users on au.User_Id equals u.Id
where u.UserName == userNameVariable
select au.Organization_Id;
var query = from tblXref in db.CrossReferences // or whatever
where innerQuery.Contains(tblXref.Organization_Id)
...;

Join two tables in Linq to SQL

Maybe a quite easy question but I'm new in Linq to SQL. I have two tables
User : UserId,name,Password,Email
USER_TABLE: Id, UserId, FirstName,LastName......
I would like a query that gives for example: userID=3 then give all the details (FirstName,LastName etc)
How can I join these two tables? I would prefer C# code if it is possible!
You do not have to do all the plumbing yourself. If you have the right foreign keys in the database you will not have to join yourself.
You can just do:
var query = from u in db.Users
select new { User = u;
FirstName = u.UserTables.FirstName }
for an inner join use something like:
var query = from u in db.Users
join ut in db.UserTables on u.UserId equals ut.UserId
select new
{
User = u,
Extra = ut
};
It's possible to join tables using linq:
E.g :
var test = (from a in DataContext.User
join b in DataContext.UserTable on a.UserId equals b.UserId
select new
{
UserId = a.UserId,
FirstName = b.FirstName
LastName = b.LastName
}).ToList();
Regards
Like this perhaps:
var joinedResult = from u in context.User
join u2 in context.UserTable on u.UserId equals u2.UserId
select new {
UserId = u.UserId,
FirstName = u2.FirstName
};
I guess your example is just an example, right? Cause it doesn't make much sense splitting that data into two tables.

Linq To SQL and Having

I am fairly new to Linq To SQL but trying to run what should be a fairly simple SQL query and can't figure out how to make it play nice in LINQ.
SELECT Users.Id, Users.Id AS Expr1, Users.FirstName, Users.LastName,
User_x_Territory.UserID
FROM Users LEFT OUTER JOIN
User_x_Territory ON User_x_Territory.UserID = Users.Id
GROUP BY Users.Id, Users.Id, Users.FirstName, Users.LastName, User_x_Territory.UserID
HAVING (COUNT(User_x_Territory.UserID) = 0)
Just trying to get all users that do not have a territory assigned, the only way to tell if they have a territory is to check the user_x_territory gerrund.
I am able to get all of the users out of my DB with this:
var users = from u in db.Users
join uXt in db.User_x_Territories on u equals uXt.User into gerr
from users in gerr.DefaultIfEmpty()
select users;
But from there I can't figure out how to add a group by/having to refine the search results to only show users with no territories.
Thanks for any help.
I suggest the following solution.
db.Users.Where(u => u.User_x_Territories.Count == 0)
edit: heh, guess someone beat me to it :(
from t in db.Users
join t0 in db.User_x_Territory on new { UserID = t.Id } equals new { UserID = t0.UserID } into t0_join
from t0 in t0_join.DefaultIfEmpty()
group new {t, t0} by new {
t.Id,
Column1 = t.Id,
t.FirstName,
t.LastName,
t0.UserID
} into g
where g.Count() == 0
select new {
Id = g.Key.Id,
Expr1 = g.Key.Id,
g.Key.FirstName,
g.Key.LastName,
UserID = g.Key.UserID
}
I don't know how efficient this is (I'm guessing not very), but you could try something like this:
var users = (from u in db.Users
join uXt in db.User_x_Territories on u equals uXt.User into gerr
from users in gerr.DefaultIfEmpty()
select u).Where(u => u.User_x_Territories.Count == 0);

Categories

Resources