开发者

Linq how to use Where on the result of Join

I need compute some join operations on my data, on the result of these operations I need to use a WHERE statement.

At the moment I use this syntax below, no errors, but result is not as expected. F开发者_开发问答or sure I need Group the result for my join and apply a query on it.

Could you please provide me an example of code? Unfortunately I'm pretty new at Linq and I'm not able to do it.

var myImagesForUser = from i in context.CmsImagesContents
                      join c in context.CmsContents on i.ContentId equals c.ContentId
                      join a in context.CmsAuthors on c.AuthorId equals a.AuthorId
                      join u in context.aspnet_Users on a.UserId equals u.UserId
                      where u.UserId == (Guid)myLoggedInUser.ProviderUserKey
                      select i;


Probably related data for current logged in user is not present in any one or more tables (Authors, Contents, ImagesContents) and you are not getting any result due to inner join on these tables. you can try outer join instead. Look at this article for left outer join in Linq queries


Hmm. I see two things which I would do different.

1.) You forgot to call .ToList() at the end in order to execute the query. Otherwise it can be, that you misuse the LINQ Statement in the processing afterwords.

 var myImagesForUser = (YOUR LINQ STATEMENT).ToList();

2.) It's strange that you need to convert the UserKey to GUID. If really needed I would do it prior the LINQ Statement and check against a local variable.

var userId = (Guid)myLoggedInUser.ProviderUserKey;
var myImagesForUser = (YOUR LINQ STATEMENT ... where u.UserId == userId ).ToList();

Perhaps this helps you to get the expected result.


where a.UserId == (Guid)myLoggedInUser.ProviderUserKey.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜