Sunday, 22 March 2015

MSCRM WITH linq examples

 ClientCredentials creden = new ClientCredentials();
        creden.UserName.UserName = "administrator";
        creden.UserName.Password = "XXXXXXXX";
        Uri url = new Uri("http://66666/CRM2013Vanilla/XRMServices/2011/Organization.svc");
        OrganizationServiceProxy proxy = new OrganizationServiceProxy(url, null, creden, null);
        proxy.EnableProxyTypes();
        IOrganizationService service = (IOrganizationService)proxy;
        Entity acc = new Entity("new_test");
       // acc.Attributes["new_name"] = "Basakar";
       // acc.Attributes["new_age"] = "Basakar";
       // acc.Attributes["new_qualification"] = "Basakar";
       // acc.Attributes["new_name"] = "Basakar";
        //acc.Attributes["FirstName "] = "Basakar";
        //acc.Attributes["LastName "] = "Basakar";
       // acc.Attributes["new_gender"] =Convert.ToInt32(1); //new OptionSetValue(1);

      //  service.Create(acc);

       
       OrganizationServiceContext svcContext = new OrganizationServiceContext(service);
      // var rest = from s in svcContext.CreateQuery("new_test") orderby s["name"] select s;

       var rest = from s in svcContext.CreateQuery("new_test") select s;
      // var rest = from s in svcContext.CreateQuery("new_test") where (s["new_age"].Equals("Basakar")) select new {RR=s["new_qualification"],yy=s["new_age"]};
      // var rest = from s in svcContext.CreateQuery("new_test") where ((string)s["new_age"]).Contains("Basakar") select new { RR = s["new_qualification"], yy = s["new_age"] };
      // var rest = from s in svcContext.CreateQuery("new_test") where (s["new_age"].Equals("Basakar")) select new { RR = s["new_qualification"], yy = s["new_age"] };
      // var rest = from s in svcContext.CreateQuery("new_test") where ((string)s["new_age"]).StartsWith("B") select new { RR = s["new_qualification"], yy = s["new_age"] };

       var resta = svcContext.CreateQuery("new_test").SingleOrDefault(s=>s.GetAttributeValue<string>("new_name")=="RAVI1");
       var resta1 = svcContext.CreateQuery("new_test").SingleOrDefault(s => s.GetAttributeValue<string>("new_name") == "RAVI1");

       
        var queryAccount2 = from s in svcContext.CreateQuery("new_test")
                           where ((EntityReference)s["new_testid"]).Id.Equals(new Guid("25386067-FACA-E411-A5B7-080027974FF3"))select new
                           {
                               RR = s["new_qualification"],
                               yy = s["new_age"]
                           };
       var res = from s in svcContext.CreateQuery("new_test")
                  where s["new_name"]=="RAVI" select s;
        foreach (var a in res)
      {
          string name = a.Attributes["new_name"].ToString();
          int name1 = ((Microsoft.Xrm.Sdk.OptionSetValue)(a.Attributes["new_qualifications"])).Value;//a.Attributes["new_qualification"].ToString();
          decimal amount = ((Microsoft.Xrm.Sdk.Money)(a.Attributes["new_fee"])).Value;
      }
        var data1 = from s in svcContext.CreateQuery("account")
                    join t in svcContext.CreateQuery("account")
                      on (((EntityReference)s["new_customers"]).Name) equals t["name"]
                    select new
                    {
                        RR = s["new_fee"],
                       // yy = t["name"]
                    };
                  //where s["new_name"] == "RAVI"
                  //select s;
        var query_join2 = from c in svcContext.CreateQuery("contact")
                          join a in svcContext.CreateQuery("account")
                          on c["contactid"] equals a["primarycontactid"]
                          select new
                          {
                              contact_name = c["fullname"],
                              account_name = a["name"]
                          };
        var list_getattrib1 = (from c in svcContext.CreateQuery("contact")
                                   where c.GetAttributeValue<Guid?>("contactid") == new Guid("E3D5BBE8-4FD0-E411-B6BA-080027974FF3")
                                   select new
                                   {
                                       FirstName = c.GetAttributeValue<string>("firstname"),
                                       LastName = c.GetAttributeValue<string>("lastname")
                                   }).ToList();
            foreach (var c in list_getattrib1)
            {
             

            }
       



       var fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='new_test'>
    <attribute name='new_qualifications' />
    <attribute name='new_qualification' />
    <attribute name='new_pass' />
    <attribute name='new_joindate' />
    <attribute name='new_gender' />
    <attribute name='new_fee' />
    <attribute name='transactioncurrencyid' />
    <attribute name='new_customers' />
    <attribute name='new_age' />
    <attribute name='new_testid' />
    <order attribute='new_name' descending='false' />
    <filter type='and'>
      <condition attribute='new_testid' operator='eq' uiname='RAVI' uitype='new_test' value='{FB4E79B1-88CD-E411-956C-080027974FF3}' />
    </filter>
  </entity>
</fetch>";



     //    QueryExpression query = new QueryExpression
     //{
     //     EntityName = "new_test",
     //     ColumnSet = columns,
     //     Criteria = new FilterExpression
     //     {
     //          Conditions =
     //          {
     //               new ConditionExpression
     //               {
     //                    AttributeName = "attributeName",
     //                    Operator = ConditionOperator.Equal,
     //                    Values = {"attributeValue"}
     //               }
     //          }
     //     };
     //  }

       EntityCollection entities = service.RetrieveMultiple(new FetchExpression(fetch));

       foreach (var dt in entities.Entities)
       {
           acc["new_name"] = dt.Attributes["new_name"];
           acc["new_qualifications"] = dt["new_qualifications"];
           acc["new_pass"] = dt["new_pass"];
           acc["new_customers"] = dt["new_customers"];
           acc["new_fee"] = new Money(9000);//dt["new_fee"];
           acc["new_joindate"] = dt["new_joindate"];
          // acc["new_testid"] = dt["new_testid"];
           service.Create(acc);//new_joindate//new_testid
          // service.Update(acc);
           //customer.Name();
           EntityReference customer = (EntityReference)dt.Attributes["new_customers"];
           string Name = customer.Name.ToString();
           OptionSetValue optionState = dt.Attributes["new_qualifications"] as OptionSetValue;
           int opt = optionState.Value;
           string text =dt.Attributes["new_pass"].ToString();
           string tttt = dt.Attributes["new_fee"].ToString();
           decimal amount = ((Microsoft.Xrm.Sdk.Money)(dt.Attributes["new_fee"])).Value;
           DateTime date=Convert.ToDateTime(dt.Attributes["new_joindate"]);
           bool data=dt.GetAttributeValue<bool>("new_pass");
           DateTime dt1 = dt.GetAttributeValue<DateTime>("new_joindate");
           int dst = Convert.ToInt32(dt.GetAttributeValue<OptionSetValue>("new_qualifications").Value);
       }

No comments:

Post a Comment