栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

Dynamics 365——查询

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Dynamics 365——查询

Dynamics 365---三种实体记录查询方式
    • LINQ 查询
    • FetchXml查询
    • QueryExpression 查询
    • 那种查询方式更佳

LINQ 查询
var queryopportunityproduct = from c in organizationServiceContext.CreateQuery("lsh_opp_product")
                              join a in organizationServiceContext.CreateQuery("lsh_order")
                              on c["lsh_order_id"] equals a["lsh_orderid"]
                              where a["lsh_evolve"].Equals("100000001")
                              where c["lsh_products_type"].Equals("100000000")
                              select new
                              {
                                  opportunityproductname = c["lsh_name"],
                                  productName = c["lsh_productid"],
                                  orderName = a["lsh_name"]
                              };
foreach (var item in queryopportunityproduct)
{
    Console.WriteLine(item.opportunityproductname.ToString() + "-----" + item.orderName.ToString());
}

LINQ 查询需要引用Microsoft.Xrm.Sdk类库中的OrganizationServiceContext类。CreateQuery里放Entity的LogicalName就是数据库中的表。其余的编写与平常的LINQ没有区别,但有些LINQ的语法是有限制的,详情请参考dynamincs 365 CRM Linq晚期的用法和Linq的使用方法

FetchXml查询
string fetch = "" +
                  "" +
                    "" +
                    "" +
                    "" +
                      "" +
                    "" +
                    "" +
                      "" +
                      "" +
                        "" +
                      "" +
                    "" +
                  "" +
                "";
FetchExpression fetchxml = new FetchExpression(fetch);
DataCollection entities = server.RetrieveMultiple(fetchxml).Entities;
if (entities != null && entities.Count > 0)
{
    foreach (Entity item in entities)
    {
        Console.WriteLine(item["lsh_name"].ToString() +"-----"+ ((AliasedValue)item["ac.lsh_name"]).Value);
    }
}

FetchXml是在系统里配好条件直接拿来用的,在这不做过多解释。

QueryExpression 查询
QueryExpression query = new QueryExpression
{
    EntityName = "lsh_opp_product",
    ColumnSet = new ColumnSet("lsh_name", "lsh_productid"),
    Criteria = new FilterExpression
    {
        FilterOperator = LogicalOperator.And,
        Conditions ={
             new ConditionExpression{
                  AttributeName = "lsh_products_type",
                  Operator = ConditionOperator.Equal,
                  Values = {100000000}
             }
        }
    },
    LinkEntities = {
        new LinkEntity{
            LinkToEntityName ="lsh_order",
            LinkToAttributeName = "lsh_orderid",
            LinkFromEntityName = "lsh_opp_product",
            LinkFromAttributeName = "lsh_order_id",
            Columns = new ColumnSet("lsh_name"),
            LinkCriteria = new FilterExpression
                {
                    FilterOperator = LogicalOperator.And,
                    Conditions =
                    {
                        new ConditionExpression
                        {
                             AttributeName = "lsh_evolve",
                             Operator = ConditionOperator.Equal,
                             Values = {100000001 }
                        }
                    }
            }
        }
    }
};
EntityCollection entities = server.RetrieveMultiple(query);
foreach (var item in entities.Entities)
{
    Console.WriteLine(item["lsh_name"].ToString() + "-----" + ((AliasedValue)item["lsh_order1.lsh_name"]).Value);
}

在这里稍作解释,用到的属性太多

属性详情
EntityName主实体
ColumnSet需要查询字段的系统架构名称
AttributeName条件字段名称
Operator比较符
Values比较值
LinkEntities链接实体
LinkToEntityName链接实体架构名称
LinkToAttributeName链接属性列
LinkFromEntityName主实体名称
LinkFromAttributeName主实体链接实体字段名称
LinkCriteria链接表的条件
那种查询方式更佳

说完了这三种查询方式,那哪种方式性能更佳,编写更方便呢?上图片

第一种为LINQ 查询、第二种为FetchXml查询、第三种为QueryExpression 查询。数据方面我准备了5000+条。从性能上来看,QueryExpression 性能更佳,但是编写代码复杂,可以把QueryExpression 查询优化成一个方法,每次就不用写这么多代码啦! 不过有不写代码的方式(FetchXml查询),为何不直接用呢!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/910615.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号