Dynamic对实体进行增删改查
Posted yaohongtao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dynamic对实体进行增删改查相关的知识,希望对你有一定的参考价值。
1.查询
(1)
public PersisitBroker broker;
public SyncAccountCommand()
{
broker = new PersisitBroker("CRM");
}
string sql = string.Format(@"SELECT new_userid, isnull(new_name, ‘ ‘) AS new_name, new_email
, new_userrole, isnull(new_phone, ‘ ‘) AS new_phone, statecode
FROM new_user
WHERE new_email IS NOT NULL
AND new_userrole IS NOT NULL");
DataTable dt = this.broker.Query(sql);
(2)
QueryExpression qe = new QueryExpression("new_yaoorder");
qe.ColumnSet.AllColumns = true;
qe.Criteria.AddCondition("new_yaoorderid", ConditionOperator.Equal, currentEntity.GetAttributeValue<EntityReference>("new_yaoorderid").Id);
qe.NoLock = true;
qe.TopCount = 1;
var ec = orgSvc.RetrieveMultiple(qe);
//单价金额
ec.Entities[0]["new_sumamount"] = (decimal)999;
//优惠金额
decimal new_sumamount = decimal.Parse(ec.Entities[0]["new_sumamount"].ToString());
//应付金额 = 单价金额 - 优惠金额
ec.Entities[0]["new_paymentamount"] = decimal.Parse(ec.Entities[0]["new_sumamount"].ToString()) - new_sumamount;
try
{
orgSvc.Update(ec[0]);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.ToString());
}
(3)
QueryExpression queryEmail = new QueryExpression("new_tmpl_mail");
queryEmail.ColumnSet = new ColumnSet(new string[] { "new_topic", "new_memo" });
queryEmail.Criteria.AddCondition("new_type", ConditionOperator.Equal, 1);
queryEmail.Criteria.AddCondition("new_code", ConditionOperator.Equal, "E-mail template");
queryEmail.AddOrder("createdon", OrderType.Descending);
EntityCollection emailEnt = OrganizationService.RetrieveMultiple(queryEmail);
if (emailEnt != null && emailEnt.Entities != null && emailEnt.Entities.Count > 0)
{
Entity entEmail = emailEnt[0];
var new_topic = entEmail.GetAttributeValue<string>("new_topic");
var subject = string.Format(new_topic, model.UserName);
var new_memo = entEmail.GetAttributeValue<string>("new_memo");
}
(4)
FetchExpression fetchXml = new FetchExpression(string.Format(@"<fetch version=‘1.0‘ output-format=‘xml-platform‘ mapping=‘logical‘ distinct=‘false‘>
<entity name=‘new_systemparameter‘>
<attribute name=‘new_systemparameterid‘ />
<attribute name=‘new_value‘ />
<filter type=‘and‘>
<condition attribute=‘statecode‘ operator=‘eq‘ value=‘0‘ />
<condition attribute=‘new_name‘ operator=‘eq‘ value=‘{0}‘ />
</filter>
</entity>
</fetch>", name));
EntityCollection ec = OrganizationService.RetrieveMultiple(fetchXml);
if (ec != null && ec.Entities != null && ec.Entities.Count > 0)
{
value = ec.Entities[0]["new_value"].ToString();
}
2.增加
(1)
this.OrganizationService.Create(entity);
3.修改
(1)
string updateSql = string.Format(@"
update new_user
set new_issend={0}
where new_userId=‘{1}‘", 0, model.OutterUserId);
broker.Execute(updateSql);
(2)
EntityCollection entityList = this.OrganizationService.RetrieveMultiple(expression);
if (entityList != null && entityList.Entities != null && entityList.Entities.Count > 0)
{
entity.Id = (Guid)entityList[0].Attributes["new_catalogueid"];
entity["new_code"] = dr["VALUE"].ToString();
entity["new_nameen"] = dr["VALUE_MEANING_EN"].ToString();
this.OrganizationService.Update(entity);
}
4.删除
以上是关于Dynamic对实体进行增删改查的主要内容,如果未能解决你的问题,请参考以下文章