Compartir a través de


Usar clase de entidad de enlace en tiempo de ejecución con una consulta LINQ

 

Publicado: enero de 2017

Se aplica a: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

En Microsoft Dynamics 365 y Microsoft Dynamics 365 (online), puede usar el enlace en tiempo de ejecución con las consultas de Consulta integrada del lenguaje .NET (LINQ). El enlace en tiempo de ejecución usa el nombre lógico del atributo, y se resuelve en un tiempo de ejecución.

En este tema

Uso del enlace en tiempo de ejecución en una cláusula de combinación

Uso del enlace en tiempo de ejecución en una combinación izquierda

Uso del enlace en tiempo de compilación y del método Contains

Uso del enlace en tiempo de ejecución y operador Not Equals

Uso del método de GetAttributeValue

Uso del enlace en tiempo de ejecución en una cláusula de combinación

Los siguientes ejemplos muestran cómo usar el enlace en tiempo de ejecución en la cláusula de join de una consulta de LINQ.

Recuperar el nombre completo del contacto que representa el contacto principal de una cuenta y el nombre de la cuenta.


using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(_serviceProxy))
{
 var query_join2 = from c in orgSvcContext.CreateQuery("contact")
                   join a in orgSvcContext.CreateQuery("account")
                   on c["contactid"] equals a["primarycontactid"]
                   select new
                   {
                    contact_name = c["fullname"],
                    account_name = a["name"]
                   };
 foreach (var c in query_join2)
 {
  System.Console.WriteLine(c.contact_name + "  " + c.account_name);
 }
}

Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
 Dim query_join2 = From c In orgSvcContext.CreateQuery("contact") _
                   Join a In orgSvcContext.CreateQuery("account") _
                   On c("contactid") Equals a("primarycontactid") _
                   Select New With {Key .contact_name = c("fullname"),
                                    Key .account_name = a("name")}
 For Each c In query_join2
  Console.WriteLine(c.contact_name.ToString() & "  " _
                    & c.account_name.ToString())
 Next c
End Using

Recupere los datos del contacto, de la cuenta y de los clientes potenciales donde el cliente potencial fue el cliente potencial original y el apellidos del contacto no es "Parker"


using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(_serviceProxy))
{
 var query_dejoin = from c in orgSvcContext.CreateQuery("contact")
                    join a in orgSvcContext.CreateQuery("account") 
                    on c["contactid"] equals a["primarycontactid"]
                    join l in orgSvcContext.CreateQuery("lead") 
                    on a["originatingleadid"] equals l["leadid"]
                    where (string)c["lastname"] != "Parker"
                    select new { Contact = c, Account = a, Lead = l };
 foreach (var c in query_dejoin)
 {
  System.Console.WriteLine(c.Account.Attributes["name"] + " " + 
   c.Contact.Attributes["fullname"] + " " + c.Lead.Attributes["leadid"]);
 }
}

Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
 Dim query_dejoin = From c In orgSvcContext.CreateQuery("contact") _
                    Join a In orgSvcContext.CreateQuery("account")
                    On c("contactid") Equals a("primarycontactid") _
                    Join l In orgSvcContext.CreateQuery("lead") _
                    On a("originatingleadid") Equals l("leadid") _
                    Select New With {Key .Contact = c,
                                     Key .Account = a,
                                     Key .Lead = l}
 For Each c In query_dejoin
  Console.WriteLine(c.Account.Attributes("name").ToString() _
                    & " " _
                    & c.Contact.Attributes("fullname").ToString() _
                    & " " _
                    & c.Lead.Attributes("leadid").ToString())
 Next c
End Using

Uso del enlace en tiempo de ejecución en una combinación izquierda

En el siguiente ejemplo se muestra cómo recuperar una lista de información de contacto y de la cuenta con una combinación izquierda. Una combinación izquierda está diseñada para devolver elementos principales con y sin elementos secundarios desde dos orígenes. Existe una correlación entre los elementos principales y los elementos secundarios, pero es posible que no existan elementos secundarios


using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(_serviceProxy))
{
 var query_join9 = from a in orgSvcContext.CreateQuery("account")
                   join c in orgSvcContext.CreateQuery("contact") 
                   on a["primarycontactid"] equals c["contactid"] into gr
                   from c_joined in gr.DefaultIfEmpty()
                   select new
                   {
                    account_name = a.Attributes["name"]
                   };
 foreach (var c in query_join9)
 {
  System.Console.WriteLine(c.account_name);
 }
}

Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
 Dim query_join9 = From a In orgSvcContext.CreateQuery("account") _
                   Group Join c In orgSvcContext.CreateQuery("contact") _
                   On a("primarycontactid") Equals c("contactid") Into gr = _
                   Group From c_joined In gr.DefaultIfEmpty() _
                   Select New With {Key .account_name = a.Attributes("name")}
 For Each c In query_join9
  Console.WriteLine(c.account_name)
 Next c
End Using

Uso del enlace en tiempo de compilación y del método Contains

El siguiente ejemplo muestran cómo usar el enlace en tiempo de ejecución con el método de Contains de una consulta de LINQ.


using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(_serviceProxy))
{
 var query_contains3 = from c in orgSvcContext.CreateQuery("contact")
                       where ((string)c["description"]).Contains("Coho")
                       select new
                       {
                        firstname = c.Attributes["firstname"],
                        lastname = c.Attributes["lastname"]
                       };
 foreach (var c in query_contains3)
 {
  System.Console.WriteLine(c.firstname + " " + c.lastname);
 }
}

Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
 Dim query_contains3 = From c In orgSvcContext.CreateQuery("contact") _
                       Where (CStr(c("description"))).Contains("Coho") _
                       Select New With
                             {Key .firstname = c.Attributes("firstname"),
                             Key .lastname = c.Attributes("lastname")}

 For Each c In query_contains3
  Console.WriteLine(c.firstname.ToString() & " " _
                    & c.lastname.ToString())
 Next c
End Using

Uso del enlace en tiempo de ejecución y operador Not Equals

El siguiente muestra cómo usar el operador Not Equals.


using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(_serviceProxy))
{
 var query_ne3 = from c in orgSvcContext.CreateQuery("contact")
                 where !c["address1_city"].Equals(null)
                 select new
                 {
                  FirstName = c["firstname"],
                  LastName = c["lastname"],
                  Address1_City = c["address1_city"]
                 };
 foreach (var c in query_ne3)
 {
  System.Console.WriteLine(c.FirstName + " " + 
   c.LastName + " " + c.Address1_City);
 }
}

Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
 Dim query_ne3 = From c In orgSvcContext.CreateQuery("contact") _
                 Where (Not c("address1_city").Equals(Nothing)) _
                 Select New With {Key .FirstName = c("firstname"),
                                  Key .LastName = c("lastname"),
                                  Key .Address1_City =
                                  c("address1_city")}
 For Each c In query_ne3
  Console.WriteLine(c.FirstName.ToString() & " " _
                    & c.LastName.ToString() & " " _
                    & c.Address1_City.ToString())
 Next c
End Using

Uso del método de GetAttributeValue

En el siguiente ejemplo se muestra cómo recuperar la información de contacto mediante el método de GetAttributeValue.


using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(_serviceProxy))
{

 var list_getattrib1 = (from c in orgSvcContext.CreateQuery("contact")
                        where c.GetAttributeValue<Guid?>("contactid") != _contactId1
                        select new { 
                         FirstName = c.GetAttributeValue<string>("firstname"), 
                         LastName = c.GetAttributeValue<string>("lastname") 
                        }).ToList();
 foreach (var c in list_getattrib1)
 {
  System.Console.WriteLine(c.FirstName + " " + c.LastName);
 }
}

Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
 Dim list_getattrib1 = ( _
     From c In orgSvcContext.CreateQuery("contact") _
     Where Not c.GetAttributeValue(Of Guid?)("contactid") _
     .Value.Equals(_contactId1) _
     Select New With
            {
             Key .FirstName = c.GetAttributeValue(Of String)("firstname"),
             Key .LastName = c.GetAttributeValue(Of String)("lastname")}
         ).ToList()
 For Each c In list_getattrib1
  Console.WriteLine(c.FirstName &amp; " " &amp; c.LastName)
 Next c
End Using

Ver también

CreateQuery<TEntity>
Crear consultas con LINQ (consulta integrada del lenguaje .NET)
Ordenar resultados mediante atributos de entidad con LINQ

Microsoft Dynamics 365

© 2017 Microsoft. Todos los derechos reservados. Copyright