I've got two entities, Users and Friendships which look like:
public class User
{
public int UserId { get; set; }
(...)
}
public class Friendship
{
public int SenderId { get; set; }
public int ReceiverId { get; set; }
(...)
}
And I would like to create simple query which in SQL would look like:
SELECT * FROM Users as U
INNER JOIN Friendships as F ON U.UserId = F.ReceiverId OR U.UserId = F.SenderId
Where U.Nick != VARIABLE
In other words I would like to select all friends of the user.
And I can't accomplish that. I've found solution where one creates two separate join queries with union and it works - but it's not efficient to create such query to db.
Joins in LINQ are always equijoins. Basically you need multiple from clauses and a where clause:
var query = from u in db.Users
where u.Nick != variable
from f in db.Friendships
where u.UserId == f.ReceiveId || u.UserId == f.SenderId
select ...;
Now in LINQ to Objects there are probably more efficient ways of doing this - but I'd expect a SQL-based LINQ provider to generate a query which has a good enough execution plan. It may not actually create a JOIN in the SQL, but I'd expect it to be the same execution plan as the join you've shown.
Simply write:
from U in db.Users
from F in Friendships.Where(x => U.UserId == F.ReceiverId || U.UserId == F.SenderId)
where U.Nick != VARIABLE
select new {u, f};
Related
I am trying to query a one to many relationship but cannot figure out how to do this. The problem I have is that the ID of the field I want to filter by lives in the join table (not the main table)...
Its probably easier to illustrate rather than explain!!
The two classes I have are
public class DbUserClient
{
public virtual string UserId { get; set; }
public virtual int ClientId { get; set; }
public virtual DateTime AssignedOn { get; set; }
public virtual DateTime? ClearedOn { get; set; }
// navigation properties
public virtual DbUser User { get; set; }
public virtual DbClient Client { get; set; }
}
and
public class DbClient
{
public virtual int ClientId {get;set;}
public virtual string EntityName { get; set; }
public virtual bool Deleted { get; set; }
// navigation properties
public ICollection<DbUserClient> UserClients { get; set; }
}
In the program I have a repository that exposes the Clients i.e.
public ObservableCollection<DbClient> Clients
{
get { return context.Clients.Local; }
}
I am binding to this which is why I am keen on querying via the Client as this will refresh my "Local" collection. However I can't seem to figure out a way to include the UserClients as well as add the "where" clause.
I have tried something like
context.Clients.Include(c => c.UserClients.Where(uc => uc.UserId == "ME"));
But this results in the following exception
"The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
Parameter name: path"
This works but unfortunately will not update my "Local" collection
from c in context.Clients
from uc in c.UserClients
where uc.ClientId == uc.ClientId && uc.UserId == "ME"
select new { c.ClientId, c.EntityName, uc.AssignedOn };
Any suggestions on where I have gone wrong?
Cheers
Abs
EDIT I : looking at the SQL Profiler the above query generates the following SQL
SELECT
[Extent1].[ClientId] AS [ClientId],
[Extent1].[EntityName] AS [EntityName],
[Extent2].[AssignedOn] AS [AssignedOn]
FROM [dbo].[Client] AS [Extent1]
INNER JOIN [dbo].[UserClient] AS [Extent2] ON [Extent1].[ClientId] = [Extent2]. [ClientId]
WHERE ([Extent2].[ClientId] = [Extent2].[ClientId]) AND (N'ME' = [Extent2].[UserId])
This is pretty simple and more or less along the lines of what I would have written myself if I was handcrafting the SQL
However although the suggested expression below works and as you pointed out populates the Local cache
context.Clients
.Where(c => c.UserClients.Any(uc => uc.UserId == userId))
.Select(c => new { DbClient = c, DbUser = c.UserClients.Where(uc => uc.UserId == userId).FirstOrDefault() }).ToList();
it produces the following SQL. This looks alot more complicated than it needs to be and I am assuming will have performance implications
exec sp_executesql N'SELECT
[Filter2].[ClientId] AS [ClientId],
[Filter2].[EntityName] AS [EntityName],
[Filter2].[Deleted] AS [Deleted],
[Limit1].[UserId] AS [UserId],
[Limit1].[ClientId] AS [ClientId1],
[Limit1].[AssignedOn] AS [AssignedOn],
[Limit1].[ClearedOn] AS [ClearedOn]
FROM (SELECT [Extent1].[ClientId] AS [ClientId], [Extent1].[EntityName] AS [EntityName], [Extent1].[Deleted] AS [Deleted]
FROM [dbo].[Client] AS [Extent1]
WHERE EXISTS (SELECT
1 AS [C1]
FROM [dbo].[UserClient] AS [Extent2]
WHERE ([Extent1].[ClientId] = [Extent2].[ClientId]) AND ([Extent2].[UserId] = #p__linq__0)
) ) AS [Filter2]
OUTER APPLY (SELECT TOP (1)
[Extent3].[UserId] AS [UserId],
[Extent3].[ClientId] AS [ClientId],
[Extent3].[AssignedOn] AS [AssignedOn],
[Extent3].[ClearedOn] AS [ClearedOn]
FROM [dbo].[UserClient] AS [Extent3]
WHERE ([Filter2].[ClientId] = [Extent3].[ClientId]) AND ([Extent3].[UserId] = #p__linq__1) ) AS [Limit1]',N'#p__linq__0 nvarchar(4000),#p__linq__1 nvarchar(4000)',#p__linq__0=N'ME',#p__linq__1=N'ME'
EDIT II : After playing around some more, I have found a solution that seems to fulfill my requirement. Looking at the SQL Profiler, I am happy with the generated SQL. This is similar to that of my orginal query.
exec sp_executesql N'SELECT
[Extent1].[ClientId] AS [ClientId],
[Extent1].[EntityName] AS [EntityName],
[Extent1].[Deleted] AS [Deleted],
[Extent2].[UserId] AS [UserId],
[Extent2].[ClientId] AS [ClientId1],
[Extent2].[AssignedOn] AS [AssignedOn],
[Extent2].[ClearedOn] AS [ClearedOn]
FROM [dbo].[Client] AS [Extent1]
INNER JOIN [dbo].[UserClient] AS [Extent2] ON [Extent1].[ClientId] = [Extent2].[ClientId]
WHERE [Extent2].[UserId] = #p__linq__0',N'#p__linq__0 nvarchar(4000)',#p__linq__0=N'ME'
I am assuming that there is no lazy loading involved here. If someone could confirm I would be grateful
context.Clients.Join
(
context.UserClients,
c => c.ClientId,
uc => uc.ClientId,
(user, usrclient) => new { DbClient = user, DbUserClient = usrclient }
).Where(uc => uc.DbUserClient.UserId == userId).Load();
You can load the clients which have at least one user with UserId = "ME":
var clients = context.Clients
.Where(c => c.UserClients.Any(uc => uc.UserId == "ME"))
.ToList();
This loads the correct clients but no user is included.
If you include the users...
var clients = context.Clients.Include(c => c.UserClients)
.Where(c => c.UserClients.Any(uc => uc.UserId == "ME"))
.ToList();
... you'll get the correctly filtered clients but it will include all users, not only the user "ME".
In order to get the users filtered as well your last approach, the projection, is the best way:
var clientsWithUser = context.Clients
.Where(c => c.UserClients.Any(uc => uc.UserId == "ME"))
.Select(c => new
{
Client = c,
User = c.UserClients.Where(uc => uc.UserId == "ME").FirstOrDefault()
})
.ToList();
This should also update the Local collection because you are loading full entities (Client and User) in the anonymous object list.
Edit
The last query in your question is fine, although it's not really the EF way to write a Join manually when you have navigation properties. The SQL and query result is most likely identical to:
context.UserClients.Include(uc => uc.Client)
.Where(uc => uc.UserId == userId)
.Load();
The Include in this query should translate into the same INNER JOIN that your hand-written LINQ Join produces.
I have the following query:
var q = from x in content_item.All()
join y in vendor.All() on x.Vendor_ID equals y.Vendor_ID into tmp
from v in tmp.DefaultIfEmpty()
select new { Z=x.Content_Item_Name,W=((v!=null)?v.Vendor_Name:"")};
when I type:
var items = q.ToList();
I got the following exception:
Expression of type 'System.Collections.Generic.IEnumerable`1[Vamp.Models.content_item]' cannot be used for parameter of type 'System.Linq.IQueryable`1[Vamp.Models.content_item]' of method 'System.Linq.IQueryable`1[<>f__AnonymousType0`2[Vamp.Models.content_item,System.Collections.Generic.IEnumerable`1[Vamp.Models.vendor]]] GroupJoin[content_item,vendor,Nullable`1,<>f__AnonymousType0`2](System.Linq.IQueryable`1[Vamp.Models.content_item], System.Collections.Generic.IEnumerable`1[Vamp.Models.vendor], System.Linq.Expressions.Expression`1[System.Func`2[Vamp.Models.content_item,System.Nullable`1[System.UInt32]]], System.Linq.Expressions.Expression`1[System.Func`2[Vamp.Models.vendor,System.Nullable`1[System.UInt32]]], System.Linq.Expressions.Expression`1[System.Func`3[Vamp.Models.content_item,System.Collections.Generic.IEnumerable`1[Vamp.Models.vendor],<>f__AnonymousType0`2[Vamp.Models.content_item,System.Collections.Generic.IEnumerable`1[Vamp.Models.vendor]]]])'
Any idea?
Note: content_item.All() is IQueryable and vendor.All() is IQueryable
Sorry I missed this question back when you asked it...
The left outer join syntax in SubSonic 3 is slightly different. I have a workaround posted as an answer to this question: Subsonic 3.0 Left Join
Hi you need to do something like this, create a getter setter as followed:
public class ReturnProperty
{
public string Z{ get; set; }
public string W{ get; set; }
}
And Change your query like this:
var q = from x in content_item.All()
join y in vendor.All() on x.Vendor_ID equals y.Vendor_ID into tmp
from v in tmp.DefaultIfEmpty()
select new ReturnProperty { Z=x.Content_Item_Name,W=((v!=null)?v.Vendor_Name:"")};
var items = q.ToList();
Hope this helps..
I have the feeling that using joins could make this cleaner
public override string[] GetRolesForUser(string username)
{
using (TemplateEntities ctx = new TemplateEntities())
{
using (TransactionScope tran = new TransactionScope())
{
int userId = (from u in ctx.Users
where u.UserName == username
select u.UserId).Single();
int[] roleIds = (from ur in ctx.UserInRoles
where ur.UserId == userId
select ur.RoleId).ToArray();
string[] roleNames = (from r in ctx.Roles
where roleIds.Contains(r.RoleId)
select r.RoleName).ToArray();
tran.Complete();
return roleNames;
}
}
}
You should be able to use the navigation properties to follow the relations instead of using the primary keys (Entity Framework will join behind the scenes for you)
If you have (and need) UserInRoles because there are other properties defined on the junction table, you can use:
return (from u in cts.Users
from ur in u.UserInRoles
from r in ur.Roles
select r.roleName).ToArray();
Otherwise make sure the N-M relation is mapped as such, and don't map the junction table. Then you can just use:
return (from u in cts.Users
from r in u.Roles
select r.roleName).ToArray();
I'm not a c# guy, but essentially you would want to do
select u.userId, ur.roleId, r.roleName
from Users u, UserInRoles ur, Roles r
where u.userId = ? and ur.userId = u.userId and r.roleId = ur.roleId;
You can also use the in syntax if you opt for nested queries.
ie: where user_id in (select userId from UserInRoles)
So I'm new to linq so be warned what I'm doing may be completely stupid!
I've got a table of caseStudies and a table of Services with a many to many relasionship
the case studies already exist and I'm trying to insert a service whilst linking some case studies that already exist to it. I was presuming something like this would work?
Service service = new Service()
{
CreateDate = DateTime.Now,
CreatedBy = (from u in db.Users
where u.Id == userId
select u).Take(1).First(),
Description = description,
Title = title,
CaseStudies = (from c in db.CaseStudies
where c.Name == caseStudy
select c),
Icon = iconFile,
FeatureImageGroupId = imgGroupId,
UpdateDate = DateTime.Now,
UpdatedBy = (from u in db.Users
where u.Id == userId
select u).Take(1).First()
};
But This isn't correct as it complains about
Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Data.Objects.DataClasses.EntityCollection'
Can somebody please show me the correct way.
Thanks in advance
Yo have to add the query result to the case studies collection instead of trying to replace it.
var service = new Service { ... };
foreach (var caseStudy in db.CaseStudies.Where(s => s.Name == caseStudyName)
{
service.CaseStudies.Add(caseStudy);
}
You can wrap this in an extension method and get a nice syntax.
public static class ExtensionMethods
{
public static void AddRange<T>(this EntityCollection<T> entityCollection,
IEnumerable<T> entities)
{
// Add sanity checks here.
foreach (T entity in entities)
{
entityCollection.Add(entity);
}
}
}
And now you get the following.
var service = new Service { ... };
service.CaseStudies.AddRange(db.CaseStudies.Where(s => s.Name == caseStudyName));
My Techie Bretheren (and Sisteren, of course!),
I have a LinqToSql data model that has the following entities:
data model http://danimal.acsysinteractive.com/images/advisor.jpg
I need to retrieve all advisors for a specific office, ordered by their sequence within the office. I've got the first part working with a join:
public static List<Advisor>GetOfficeEmployees(int OfficeID)
{
List<Advisor> lstAdvisors = null;
using (AdvisorDataModelDataContext _context = new AdvisorDataModelDataContext())
{
var advisors = from adv in _context.Advisors
join advisoroffice in _context.OfficeAdvisors
on adv.AdvisorId equals advisoroffice.AdvisorId
where advisoroffice.OfficeId == OfficeID
select adv;
lstAdvisors = advisors.ToList();
}
return lstAdvisors;
}
However, I can't seem to wrap my weary brain around the order by clause. Can anyone give some suggestions?
from adv in _context.Advisors
where adv.OfficeAdvisor.Any(off => off.OfficeId == officeID)
order adv by adv.OfficeAdvisor.First(off => off.OfficeId = officeID).Sequence
select adv;
public static List<Advisor>GetOfficeEmployees(int OfficeID)
{
List<Advisor> lstAdvisors = null;
using (AdvisorDataModelDataContext _context = new AdvisorDataModelDataContext())
{
var advisors = from adv in _context.Advisors
join advisoroffice in _context.OfficeAdvisors
on adv.AdvisorId equals advisoroffice.AdvisorId
where advisoroffice.OfficeId == OfficeID
group adv by adv.OfficeId into g
order by g.Sequence
select g;
lstAdvisors = advisors.ToList();
}
return lstAdvisors;
}
Note: I am not able to currently test this on Visual Studio but should work.
You can add an order by clause like this:
var advisors = from adv in _context.Advisors
join advisoroffice in _context.OfficeAdvisors
on adv.AdvisorId equals advisoroffice.AdvisorId
where advisoroffice.OfficeId == OfficeID
orderby advisoroffice.Sequence // < -----
select adv;