简体   繁体   English

如何筛选ListItemCollection共享点对象模型

[英]How to filter ListItemCollection sharepoint object model

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 is getting all 4 files I want to filter list using filename.if filename matches with database table file name then exclude that item from listItems listItems正在获取我要使用filename过滤列表的所有4个文件。如果filename与数据库表文件名匹配,则将该项目从listItems中排除

for example - 例如 -

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.

above is just an example I have 100's of file which I need to compare using filename and exclude from list if present into database table. 上面只是一个示例,我有100个文件,需要使用文件名进行比较,如果存在数据库表中,则需要从列表中排除。

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

How can I achieve this without foreach loop ? 我如何在没有foreach循环的情况下实现这一目标? is there anything I can use like LINQ or CamlQuery ? 有什么可以使用的,例如LINQ或CamlQuery吗?

Try changing your caml query to something like this 尝试将您的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>";

So you can query for items where the FileLeafRef field Eq uals 3.txt or 4.txt 所以,你可以查询商品, 其中 FileLeafRef方程 uals 3.txt 4.txt

But you should check which property contains the file name you are after. 但是您应该检查哪个属性包含您要使用的文件名。 In my case I needed to change FileLeafRef to Title 就我而言,我需要将FileLeafRef更改为Title

So for a dynamic list of file names, you can append a new line for each filename before executing the query. 因此,对于动态文件名列表,可以在执行查询之前为每个文件名添加新行。 Maybe something like 也许像

// 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>";

We can also use LINQ to filter results. 我们还可以使用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();

To build lambda Expressions dynamically, check the article below: 要动态构建lambda表达式,请查看以下文章:

Build Lambda Expressions Dynamically 动态构建Lambda表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM