zzMzz
zzMzz

Reputation: 467

Linq - Value of type 'System.Guid' cannot be converted to 'String'

I'm using Linqer to convert SQL to Linq:

Update EmployeeSite SET SiteId = Null 
WHERE SiteId = '86086EC5-567A-46B3-8DFC-E624F9B0324B'

Gets translated into:

Dim queryEmployeeSites = _
    From employeesites In rdc.EmployeeSites _
    Where _
      CStr(employeesites.SiteId) = "86086EC5-567A-46B3-8DFC-E624F9B0324B" _
    Select employeesites
For Each employeesites As EmployeeSite In queryEmployeeSites
    employeesites.SiteId = Nothing
Next
rdc.SubmitChanges()

But when I try to run the Linq code I get the error message:

Error Compiling Expression: Error Compiling Expression: Value of type 'System.Guid' cannot be converted to 'String'.

I am very new to Linq. Can someone please explain what is wrong?

Thanks!

Upvotes: 3

Views: 4421

Answers (2)

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47038

You can replace CStr(employeesites.SiteId) with employeesites.SiteId.ToString(), but the best is to compare the other way around

employeesites.SiteId = Guid.Parse("86086EC5-567A-46B3-8DFC-E624F9B0324B")

This way you don't run into issues with different capitalization, etc.

Upvotes: 4

msarchet
msarchet

Reputation: 15242

Dim queryEmployeeSites = _
    From employeesites In rdc.EmployeeSites _
    Where _
    employeesites.SiteId.ToString().Equals("86086EC5-567A-46B3-8DFC-E624F9B0324B") _
    Select employeesites

For Each employeesites As EmployeeSite In queryEmployeeSites
    employeesites.SiteId = Nothing
Next

rdc.SubmitChanges()

Calling ToString() instead should work, also it's better to use Equals for String Equality, or use String.Compare.

Upvotes: 3

Related Questions