带有嵌套 AND 和 OR 的多个字段的 CAML 查询 [英] CAML query with nested ANDs and ORs for multiple fields

查看:16
本文介绍了带有嵌套 AND 和 OR 的多个字段的 CAML 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写概念验证代码,以根据提供给我正在编写的高度特定搜索网络服务的关键字动态生成 CAML.我没有使用 SharePoint 提供的搜索 Web 服务来进行此证明.为了我想要实现的目标,我已经这样做了.从我所有的研究中,我找不到一个接近我想要实现的目标的例子,即检查多个字段的多个值.是的,我已经看过了我的答案,包括这个:需要建筑方面的帮助CAML 查询.

I am working on proof-of-concept code to dynamically generate CAML based on keywords provided to a highly-specific search web service that I am writing. I am not using the SharePoint-provided search web service for this proof. I have done so already for what I am trying to achieve. From all of my research, I cannot find a close example for what I am trying to achieve, which is to check multiple fields for multiple values. Yes, I have looked on SO already for my answer, including this one: Need help on building CAML Query.

话虽如此,如果可能的话,如何用 CAML 编写以下类似 SQL 的查询?

With that said, if it is possible, how can the following SQL-like query be written in CAML?

SELECT FirstName, LastName, Description, Profile
FROM SomeFakeTable
WHERE (FirstName = 'John' OR LastName = 'John' OR Description = 'John' OR Profile='John')
  AND (FirstName = 'Doe' OR LastName = 'Doe' OR Description = 'Doe' OR Profile='Doe')
  AND (FirstName = '123' OR LastName = '123' OR Description = '123' OR Profile='123')

推荐答案

由于在一个条件组 (And | Or) 中不允许放置两个以上的条件,因此您必须创建一个额外的嵌套组 (MSDN).表达式 A AND B AND C 看起来像这样:

Since you are not allowed to put more than two conditions in one condition group (And | Or) you have to create an extra nested group (MSDN). The expression A AND B AND C looks like this:

<And>
    A
    <And>
        B
        C
    </And>
</And>

您的 SQL 样例转换为 CAML(希望有匹配的 XML 标签;)):

Your SQL like sample translated to CAML (hopefully with matching XML tags ;) ):

<Where>
    <And>
        <Or>
            <Eq>
                <FieldRef Name='FirstName' />
                <Value Type='Text'>John</Value>
            </Eq>
            <Or>
                <Eq>
                    <FieldRef Name='LastName' />
                    <Value Type='Text'>John</Value>
                </Eq>
                <Eq>
                    <FieldRef Name='Profile' />
                    <Value Type='Text'>John</Value>
                </Eq>
            </Or>
        </Or>
        <And>       
            <Or>
                <Eq>
                    <FieldRef Name='FirstName' />
                    <Value Type='Text'>Doe</Value>
                </Eq>
                <Or>
                    <Eq>
                        <FieldRef Name='LastName' />
                        <Value Type='Text'>Doe</Value>
                    </Eq>
                    <Eq>
                        <FieldRef Name='Profile' />
                        <Value Type='Text'>Doe</Value>
                    </Eq>
                </Or>
            </Or>
            <Or>
                <Eq>
                    <FieldRef Name='FirstName' />
                    <Value Type='Text'>123</Value>
                </Eq>
                <Or>
                    <Eq>
                        <FieldRef Name='LastName' />
                        <Value Type='Text'>123</Value>
                    </Eq>
                    <Eq>
                        <FieldRef Name='Profile' />
                        <Value Type='Text'>123</Value>
                    </Eq>
                </Or>
            </Or>
        </And>
    </And>
</Where>

这篇关于带有嵌套 AND 和 OR 的多个字段的 CAML 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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