开发者

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

I'm using Linqer to convert SQL to Linq:

Update Em开发者_运维技巧ployeeSite 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!


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜