簡體   English   中英

是否可以將Include與字段名稱的字符串值一起使用?

[英]Is it possible to use Include with a string value of the field name?

  1. 我想使用CSOM加載SharePoint Project Server項目

     var proj = ctx.LoadQuery(ctx.Projects .Where(p => p.Id == projGuid) .Include(p => p.Id, p => p.Name)); ctx.ExecuteQuery(); 
  2. 我需要Include()是動態的,所以我這樣做了:

     Expression<Func<PublishedTask, object>>[] funcArray = new Expression<Func<PublishedTask, object>>[2]; funcArray[0] = p => p.Name; funcArray[1] = p => p.Id; var proj = ctx.LoadQuery(ctx.Projects .Where(p => p.Id == projGuid) .Include(funcArray)); ctx.ExecuteQuery(); 
  3. 現在,我僅獲得必須從字符串中加載哪個屬性的信息,因此我嘗試了如下操作:

     funcArray[0] = p => p.GetType().GetProperty("Name"); funcArray[1] = p => p.GetType().GetProperty("Id"); 

那是行不通的,而且我不確定我是否做得正確,或者是否有可能做到這一點。

您可以傳遞一個逗號分隔的字符串,並使用一些Linq拆分它們,並在查詢中包括每個函數,而不是傳遞一個函數數組:

var includeFields = "Name,Id";

var proj = ctx.Projects.GetByGuid(projGuid);

foreach (var fieldName in includeFields.Split(','))
{
    ctx.Load(proj, includes => includes[fieldName]);
}

ctx.ExecuteQuery();

您也可以使用方法“ GetByGuid”代替Linq Where。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM