动态 Linq 库 Guid 异常

发布于 2024-09-01 11:53:39 字数 844 浏览 5 评论 0原文

我在使用动态 Linq 库时遇到问题。我收到以下错误“ParserException 未由用户代码 ')”或 ','" 处理。我有一个字典,我想基于此字典创建一个查询。所以我循环遍历我的字典并附加到字符串生成器“PersonId =(字典中的 GUID)。我认为问题是,如果我出于某种原因附加到 PersonId,我似乎无法将字符串 guid 转换为 Guid,这样动态库就不会崩溃。

我尝试过将字符串 guid 转换为 guid,但没有成功。

query.Append("(PersonId = Guid(" + person.Key + ")");

query.Append("(PersonId = " + person.Key + ")");

我正在使用 VS 2010 RTM 和 RIA 服务以及实体框架 4。

//This is the loop I use
foreach (KeyValuePair<Guid, PersonDetails> person in personsDetails)
{
    if ((person.Value as PersonDetails).IsExchangeChecked)
    {
        query.Append("(PersonId = Guid.Parse(" + person.Key + ")");
    }
}

//Domain service call
 var query = this.ObjectContext.Persons.Where(DynamicExpression.ParseLambda<Person, bool>(persons));

请提供帮助,如果您知道更好的方法,我愿意接受建议。

I am having a problem with the Dynamic Linq Library. I get a the following error "ParserException was unhandled by user code ')" or ','". I have a Dicitionary and I want to create a query based on this dictionary. So I loop through my dictionary and append to a string builder "PersonId = (GUID FROM DICTIONARY). I think the problem is were I append to PersonId for some reason I can't seem to convert my string guid to a Guid so the dynamic library don't crash.

I have tried this to convert my string guid to a guid, but no luck.

query.Append("(PersonId = Guid(" + person.Key + ")");

query.Append("(PersonId = " + person.Key + ")");

I am using VS 2010 RTM and RIA Services as well as the Entity Framework 4.

//This is the loop I use
foreach (KeyValuePair<Guid, PersonDetails> person in personsDetails)
{
    if ((person.Value as PersonDetails).IsExchangeChecked)
    {
        query.Append("(PersonId = Guid.Parse(" + person.Key + ")");
    }
}

//Domain service call
 var query = this.ObjectContext.Persons.Where(DynamicExpression.ParseLambda<Person, bool>(persons));

Please help, and if you know of a better way of doing this I am open to suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

会傲 2024-09-08 11:53:39

对于与动态 linq 的 GUID 比较,请使用查询属性和 Equals() 方法,如提供的示例中所示。

var items = new[]
            {
                new { Id = Guid.Empty },
                new { Id = Guid.NewGuid() },
                new { Id = Guid.NewGuid() },
                new { Id = Guid.NewGuid() }
            };

var result = items.AsQueryable()
    .Where("Id.Equals(@0)", Guid.Empty)
    .Any();

For GUID comparison with dynamic linq use query properties and the Equals() method like in the provided sample.

var items = new[]
            {
                new { Id = Guid.Empty },
                new { Id = Guid.NewGuid() },
                new { Id = Guid.NewGuid() },
                new { Id = Guid.NewGuid() }
            };

var result = items.AsQueryable()
    .Where("Id.Equals(@0)", Guid.Empty)
    .Any();
尴尬癌患者 2024-09-08 11:53:39

使用参数化查询,例如:

var query = this.ObjectContext.Persons.Where(
     "PersonId = @1", new [] { person.Key } );

Use a parameterized query, e.g.:

var query = this.ObjectContext.Persons.Where(
     "PersonId = @1", new [] { person.Key } );
狼性发作 2024-09-08 11:53:39

你尝试过吗(注意额外的')')。

   query.Append("(PersonId = Guid(" + person.Key + "))");

Did you try(notice the extra ')' ).

   query.Append("(PersonId = Guid(" + person.Key + "))");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文