GUID在Where子句中 [英] GUID In Where Clause

查看:133
本文介绍了GUID在Where子句中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


使用以下VB.Net代码从成员资格提供程序中提取UserID时然后在where子句中使用GUID我得到以下错误:



字符文字必须只包含一个字符


描述:发生未处理的异常在执行当前Web请求期间。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息: System.Web.Query.Dynamic.ParseException:字符文字必须只包含一个字符



以下是我用来提取数据的代码:



Dim UserGuid As Guid = DirectCast ( Membership.GetUser.ProviderUserKey,Guid)


LinqSalesCustomers.Where = 字符串 .Format( " UserID ='{0}'" ,TypeDescriptor.GetConverter(UserGuid).ConvertTo(UserGuid, GetType 字符串 )))



我尝试了单引号,大括号{}等的每种组合,但无法使其工作。此外,当我使用数据上下文对象时,我可以将db.log设置为等于TextWriter对象,并查看Linq正在生成的SQL,但是当我使用LinqDataSource时,我似乎无法弄清楚如何将Linq调试为SQL。如何访问LinqDataSource连接的datacontext对象,以便设置日志输出?



谢谢提前获取任何帮助。

解决方案

LINQ数据源中的Where属性使用非VB的迷你表达式语言。单引号文字是字符文字,因此您只能在它们之间放置一个字符。我不记得有一个你可以使用的guid文字。但是,LINQ数据源具有用于处理这些表达式的参数的内置机制。你不应该使用string.Format来构建一个带有guid的表达式字符串。



您应该可以这样做:



LinqSalesCustomers.Where = " UserID = @id " ;;


...


LinqSalesCustomers.WhereParameters.Add(" id",UserGuid);


< p align = left>



 

When using the following VB.Net code to pull the UserID from the Membership provider and then use the GUID in a where clause I get the following error:

 

Character literal must contain exactly one character

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.Query.Dynamic.ParseException: Character literal must contain exactly one character

 

Here is the code that I'm using to pull the data:

 

Dim UserGuid As Guid = DirectCast(Membership.GetUser.ProviderUserKey, Guid)

LinqSalesCustomers.Where = String.Format("UserID = '{0}'", TypeDescriptor.GetConverter(UserGuid).ConvertTo(UserGuid, GetType(String)))

 

I've tried every combination of single quotes, braces {}, etc. and can't get this to work. Also, when I use a data context object I can set db.log equal to a TextWriter object and see the SQL that Linq is generating but I can't seem to figure out how to debug Linq to SQL when I use a LinqDataSource. How do I access the datacontext object that the LinqDataSource is connecting to so I can set the log output?

 

Thanks in advance for any help.

解决方案

The Where property on the LINQ data source uses a mini expression language that is not VB.  Single quote literal are character literals, so you can only put one character between them. I don't recall that there is a guid literal you can use.  However, the LINQ data source has a built-in mechanism for handling parameters for these expressions.  Youl should not have to be using string.Format to build an expression string with the guid in it.

 

You should be able to do something like this:

 

LinqSalesCustomers.Where = "UserID = @id";

...

LinqSalesCustomers.WhereParameters.Add("id", UserGuid);

 

 


这篇关于GUID在Where子句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆