I am new to linq. I need to run a query that combines two columns ( AnonymousUser.AnonymousIdis uniqueidentifierand comment.UserId is nvarchar(100)), something like below:
using (CommentEntities db = new CommentEntities())
{
IQueryable<Comment> filteredComments = this.CommentGetList(...);
var query = from comment in filteredComments
join user in db.AnonymousUsers on comment.UserId equals user.AnonymousId into gj
from userNull in gj.DefaultIfEmpty()
select new CommentWithName
{
Comment = comment,
UserId = comment.UserId,
FirstName = (userNull == null ? "" : userNull.Name),
LastName = "",
Email = (userNull == null ? "" : userNull.Email)
};
return query.ToList();
}
At first I was happy to write a query using .ToString()! As it turned out, the entity structure does not know how to translate it to sql. The same is true for Guid.Parse(string). In addition, new Guid(string)it cannot be used in linq for entities (only constructors without parameters are allowed)!
So, after searching, I found out that this cannot be done in EF 4.0! I transferred my code to a stored procedure, and I don’t really like it.
Is it possible to describe an entity structure for use CASTin SQL?
- ? , ?
. GO. .