将查询表达式转换为Linq C#

时间:2018-07-12 15:42:39

标签: c# linq

我无法转换在这里找到的查询表达式:http://www.oak3.org/crm/workflow-activity-checking-duplicate-instances/ 进入linq查询。简而言之,目标是如果同时触发两个工作流,则只让其中一个按最新排序。目前,我的linq版本有时会取消一个工作流程,即使它是唯一正在运行的工作流程。我知道我需要以某种方式利用输入参数(以告诉它要比较的工作流程),而且我敢肯定还会有更多问题。任何帮助是极大的赞赏。

        [Input("Current workflow")]
        [ReferenceTarget("workflow")]
        public InArgument<EntityReference> workflowReferenceInput { get; set; } 
        [Output("Is first workflow")]
    public OutArgument<Boolean> isFirstWorkflow { get; set; }

    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"];
            isFirstWorkflow.Set(executionContext, false);

            var wfReference = workflowReferenceInput.Get(executionContext);
            var wfEntity = service.Retrieve("workflow", wfReference.Id, new ColumnSet ( "name" ));

            ConditionExpression ce1 = new ConditionExpression();
            ce1.AttributeName = "statuscode";
            ce1.Operator = ConditionOperator.In;
            ce1.Values.Add(0);
            ce1.Values.Add(10);
            ce1.Values.Add(20);
            ce1.Values.Add(21);
            ce1.Values.Add(22);
            FilterExpression fe1 = new FilterExpression();
            fe1.Conditions.Add(ce1);

            QueryExpression qe = new QueryExpression();
            qe.EntityName = AsyncOperation.EntityLogicalName;
            qe.ColumnSet = new ColumnSet();
            qe.Criteria = new FilterExpression();
            qe.Criteria.AddFilter(fe1);
            var childFilter = qe.Criteria.AddFilter(LogicalOperator.And);
            childFilter.AddCondition("operationtype", ConditionOperator.Equal, 10);
            childFilter.AddCondition("name", ConditionOperator.Equal, wfEntity["name"]);

            LinkEntity link = new LinkEntity
            {
                LinkFromEntityName = AsyncOperation.EntityLogicalName,
                LinkToEntityName = Quote.EntityLogicalName,
                LinkFromAttributeName = "regardingobjectid",
                LinkToAttributeName = "quoteid"
            };
            link.LinkCriteria.AddCondition("quoteid", ConditionOperator.Equal, context.PrimaryEntityId.ToString());

            DataCollection<Entity> result = service.RetrieveMultiple(qe).Entities;

            var list = result.ToList().OrderBy(c => c.Id);
            for (var i = 0; i < list.Count(); i++)
            {
                var item = list.ElementAt(i);
                if(item.Id == context.OperationId && i == 0)
                {
                    isFirstWorkflow.Set(executionContext, true);
                }
            }

        }
        catch (Exception e)
        {
            throw new InvalidPluginExecutionException(e.Message);
        }
    }

对此:

 public class WorkflowChecker: CodeActivity
{
    [Input("Current workflow")]
    [ReferenceTarget("workflow")]
    public InArgument<EntityReference> workflowReferenceInput { get; set; }

    [Output("Is first workflow")] public OutArgument<Boolean> isFirstWorkflow { get; set; }

    protected override void Execute(CodeActivityContext executionContext)
    {
        IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

        try
        {                
            var ok = WorkflowChecker(context.PrimaryEntityId, context.OperationId);

            isFirstWorkflow.Set(executionContext, ok);
        }
        catch (Exception e)
        {
            throw new InvalidPluginExecutionException(e.Message);
        }

    }
}

调用:

 public static bool WorkflowChecker(Guid workflowContextId,
        Guid asyncOperationId)
    {
        var someReturnValue = false;
        var listOfWorkflowIds = new List<Guid>();

        try
        {
            var query = from async in AsyncOperationSet
                join b in BSet on async.RegardingObjectId.Id equals b.Id                        
                where b.Id.Equals(workflowContextId)
                      && (async.StateCode.Equals(0)
                          || async.StateCode.Equals(1)
                          || async.StateCode.Equals(2))
                select new {async.AsyncOperationId};
            foreach (var x in query)
            {
                if (x.AsyncOperationId != Guid.Empty)
                {
                    listOfWorkflowIds.Add(x.AsyncOperationId.Value);
                }
            }

            listOfWorkflowIds.Sort();

            if (listOfWorkflowIds.First() == asyncOperationId)
            {
                someReturnValue = true;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Error in Workflow Checker: " + e);
        }


        return someReturnValue;
    }

1 个答案:

答案 0 :(得分:0)

我已将以下几行添加到工作流库中,然后将参数传递给调用以按名称进行比较,到目前为止,它似乎已成功运行:

var wfReference = workflowReferenceInput.Get(executionContext);
var wfEntity = service.Retrieve("workflow", wfReference.Id, new ColumnSet("name"));
var test =  wfEntity["name"];