FetchXML貨幣字段求和,是對貨幣的base字段求和,所以求和結果需要乘以匯率。
即便求和的字段爲貨幣字段,不是對應的base字段,求和結果仍然是base字段的求和結果。
所以,對於用FetchXML做貨幣字段的求和,建議直接用base字段,結果乘以匯率。以免產生歧義和誤導。
對於貨幣字段的FetchXml的查詢,顯示的結果就是字段的值,並不是base字段的值。
另,查詢和求和FetchXML的寫法也有所不同,求和會有aggregate='true'。
Demo FetchXML求和
string priceEvaluationProductFetchXml = @"<fetch mapping='logical' distinct='false' aggregate='true'>
<entity name='new_priceevaluationproduct'>
<attribute name='new_productquotation_base' alias='sum1' aggregate='sum'/>
<attribute name='new_toolquotation_base' alias='sum2' aggregate='sum'/>
<attribute name='new_feequotation_base' alias='sum3' aggregate='sum'/>
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0'/>
<condition attribute='new_priceevaluationid' operator='eq' value='{0}'/>
</filter>
</entity>
</fetch>";
FetchExpression fetch_query = new FetchExpression(string.Format(priceEvaluationProductFetchXml, postEn.GetAttributeValue<EntityReference>("new_priceevaluationid").Id));
EntityCollection result = plugin.SysService.RetrieveMultiple(fetch_query);
if (result.Entities.Count > 0 && result.Entities[0].Attributes.Count>0)
{
Entity ent = result.Entities[0];
decimal exchangeratereference = decimal.Parse(postEn["exchangerate"].ToString());
Entity ent_priceEvaluation = new Entity("new_priceevaluation");
ent_priceEvaluation.Id = postEn.GetAttributeValue<EntityReference>("new_priceevaluationid").Id;
ent_priceEvaluation["new_partprice"]=ent.Contains("sum1") ? new Money((((Money)((AliasedValue)ent["sum1"]).Value).Value*exchangeratereference)) : null;
ent_priceEvaluation["new_toolprice"] = ent.Contains("sum2") ? new Money(((Money)((AliasedValue)ent["sum2"]).Value).Value) : null;
ent_priceEvaluation["new_developdesignprice"] = ent.Contains("sum3") ? new Money(((Money)((AliasedValue)ent["sum3"]).Value).Value) : null;
ent_priceEvaluation["new_logicrecaculate"] = true;
plugin.SysService.Update(ent_priceEvaluation);
}
FetchXML查詢
string fetchQuery = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='new_priceevaluationdetail'>
<attribute name='new_name' />
<attribute name='createdon' />
<attribute name='new_productquotation' />
<attribute name='new_totalcost' />
<attribute name='new_productioncost' />
<attribute name='new_expenseratio' />
<attribute name='new_grossmargin' />
<attribute name='new_toolcost' />
<attribute name='new_toolquotation' />
<attribute name='new_costevaluationid' />
<attribute name='new_partinfoid' />
<attribute name='new_accountpartname' />
<attribute name='new_feequotation' />
<attribute name='new_priceevaluationdetailid' />
<order attribute='createdon' descending='true' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
<condition attribute='new_priceevaluationid' operator='eq' value='{0}' />
</filter>
<link-entity name='new_costevaluation' from='new_costevaluationid' to='new_costevaluationid' alias='af'>
<filter type='and'>
<condition attribute='new_productprojectid' operator='eq' value='{1}' />
</filter>
</link-entity>
</entity>
</fetch>";
FetchExpression fetchExp = new FetchExpression(String.Format(fetchQuery, postEn.GetAttributeValue<EntityReference>("new_priceevaluationid").Id,postEn.GetAttributeValue<EntityReference>("new_productprojectid").Id));
EntityCollection result = plugin.SysService.RetrieveMultiple(fetchExp);