【问题标题】:How to filter ListItemCollection sharepoint object model如何过滤 ListItemCollection 共享点对象模型
【发布时间】:2018-05-02 05:44:08
【问题描述】:
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                            <Query>
                            </Query>
                        </View>";
camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery(); 

listItems 正在获取我想使用文件名过滤列表的所有 4 个文件。如果文件名与数据库表文件名匹配,则从 listItems 中排除该项目

例如——

4 files - 1.txt 2.txt 3.txt 4.txt
in `database` table if `1.txt and 2.txt` is present then it will match with listItems filename and need to exclude these two items from listItems.

以上只是一个示例,我有 100 个文件,我需要使用文件名进行比较,如果存在于数据库表中,则从列表中排除。

So we listItems is having only 2 items - 3.txt 4.txt

如何在没有 foreach 循环的情况下实现这一点?有什么我可以使用的,比如 LINQ 或 CamlQuery 吗?

【问题讨论】:

    标签: c# sharepoint sharepoint-object-model


    【解决方案1】:

    尝试将您的 caml 查询更改为类似的内容

    @"<View Scope='RecursiveAll'>
        <Query>
        <Where>
            <Or>
                <Eq><FieldRef Name='FileLeafRef'/><Value>3.txt</Value></Eq>
                <Eq><FieldRef Name='FileLeafRef'/><Value>4.txt</Value></Eq>
            </Or>
        </Where>                            
        </Query>
    </View>";
    

    因此您可以查询 where FileLeafRef 字段 Equals 3.txt 4.txt 的项目

    但是您应该检查哪个属性包含您要查找的文件名。在 我的情况我需要将FileLeafRef 更改为Title

    因此,对于文件名的动态列表,您可以在执行查询之前为每个文件名附加一个新行。也许像

    // Declare the query string
    var queryString = "<View Scope='RecursiveAll'><Query><Where><or>";
    
    // For each filename, append a new <eq> element containing the relevant details
    foreach (var filename in fileNames) { 
        // Process string here, eg check if its in the db or whatever you need to do
        queryString += $"<Eq><FieldRef Name='FileLeafRef'/><Value>{filename}</Value></Eq>";
    }
    
    // Append the closing tags of the rest of the query
    var queryString += "</or><Where></Query></View>";
    

    【讨论】:

    • 但文件名必须是动态的,并且还必须与数据库表值进行比较。 (我有 100 个文件到列表中)
    【解决方案2】:

    我们也可以使用 LINQ 来过滤结果。

    CamlQuery camlQuery = new CamlQuery();
    camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                          <Query>
                          </Query>
                      </View>";
    camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
    ListItemCollection listItems = list.GetItems(camlQuery);
    clientContext.Load(listItems);
    clientContext.ExecuteQuery();
    
    var items = listItems.Where(i => i.FieldValues["FileLeafRef"].ToString().Equals("1.txt")||i.FieldValues["FileLeafRef"].ToString().Equals("2.txt")).ToList();
    

    要动态构建 lambda 表达式,请查看以下文章:

    Build Lambda Expressions Dynamically

    【讨论】:

    • 谢谢,但我有 100 个文件名要比较,所以我会动态而不是硬核。
    • 查看我回复中的文章,根据数据库中的数据表动态构建表达式。
    • 执行时出错listItems.Where {"不支持指定的方法。"}
    • listItems 上有隐藏循环。这也是糟糕的设计。您可以达到查询阈值。
    • 如果你有大数据,正如 Bassie 的回复,你可以使用 CAML 来实现。构建 CAML 参考shahjinesh11.wordpress.com/2013/12/26/…
    猜你喜欢
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 2018-12-01
    • 2010-11-19
    • 2011-07-19
    • 1970-01-01
    • 2019-07-26
    • 2010-10-22
    相关资源
    最近更新 更多