以下是我的QE,以使用foreach循环获取需要的列的结果集:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using Microsoft.Xrm.Sdk.Query;
namespace UpdtPrntInvQuants
{
public class UpdtPrntInvQuants : CodeActivity
{
protected override void Execute(CodeActivityContext executionContext)
{
ITracingService tracer = executionContext.GetExtension<ITracingService>();
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
Entity entity = (Entity)context.InputParameters["Target"];
//TODO: Do stuff
Guid shiGuid = entity.Id;
QueryExpression qe0 = new QueryExpression();
qe0.EntityName = entity.LogicalName;
qe0.ColumnSet = new ColumnSet();
qe0.ColumnSet.Columns.Add("new_shid");
qe0.ColumnSet.Columns.Add("new_name");
qe0.ColumnSet.Columns.Add("new_claimed");
qe0.ColumnSet.Columns.Add("new_ii");
qe0.ColumnSet.Columns.Add("new_prod");
qe0.Criteria = new FilterExpression();
qe0.Criteria.AddCondition("new_shid", ConditionOperator.Equal, shiGuid);
LinkEntity ii = new LinkEntity("new_sh", "new_ii", "new_prod", "new_prod", JoinOperator.Inner);
ii.Columns = new ColumnSet("new_qohm", "new_qoht", "new_qohg");
ii.EntityAlias = "ii";
qe0.LinkEntities.Add(ii);
EntityCollection shis = service.RetrieveMultiple(qe0);
foreach (var shi in shis.Entities)
{
tracer.Trace("SHI: ");
tracer.Trace("new_shId: " + shI["new_shid"]);
tracer.Trace("new_name: " + shI["new_name"]);
tracer.Trace("new_claimed: " + shI["new_claimed"]);
tracer.Trace("II: ");
tracer.Trace("ii qohm: " + (shI.Attributes["ii.new_qohm"] as AliasedValue).Value);
tracer.Trace("ii qoht: " + (shI.Attributes["ii.new_qoht"] as AliasedValue).Value); ;
tracer.Trace("ii qohg: " + (shI.Attributes["ii.new_qohg"] as AliasedValue).Value);
}
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
}
}
}
如您所见,我可以在上面的循环中从链接的实体获取值:
tracer.Trace("ii qohm: " + (shI.Attributes["ii.new_qohm"] as AliasedValue).Value);
很难将其设置为来自链接实体的值。
所以基本上我想举个例子:
(shI.Attributes["ii.new_qohm"] as AliasedValue).Value) = shI["new_claimed"]
是否在循环中?
任何建议都是很棒的。我已经尝试过setAttributeValue
。
答案 0 :(得分:1)
正如所有人都说过的那样,您意识到,service.RetrieveMultiple
使用QueryExpression
会给您EntityCollection
,如果您想更新每个实体,则组成一个属性最少的新Entity对象,并使用service.Update
。