Linq joins guid and string column

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())
        {
            // filteredComments is a query that is not run until the next .ToList()
            IQueryable<Comment> filteredComments = this.CommentGetList(...);
            var query = from comment in filteredComments
                         // following line is the syntax error, because columns' types don't match
                         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. .

+5
2

toList() . :

var Product = db.Products.Where(p => p.ProductId == Guid.Parse("B4E913F9-166C-49BA-AADE-6DB889D1756F")).Single();

# LINQ to Entities "System.Guid Parse" (System.String) ',

:

var Product = db.Products.ToList().Where(p => p.ProductId == Guid.Parse("B4E913F9-166C-49BA-AADE-6DB889D1756F")).Single()

p.s: , lazyloading, eagerloading .Include .ToList().

0

list - , Guid , , UserId, UserId, int, :

     int output = 0;

     var secondList = list.Where(x=>!int.TryParse(x.UserID, out output))
                     .Select(x=>new {Comment = x, ID = new Guid(x.UserID))
                     .ToList();

db secondList.

0

All Articles