Produced by Araxis Merge on 2/1/2017 2:56:34 PM Eastern Standard Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | C:\Araxis_Merge_Comprasion\Pub_un\BTSSS_CIF_122016.zip\BTSSS_CIF_12_20_16\clean\CRM\trunk\SDK\SampleCode\CS\GeneralProgramming\Queries | LINQExamples.cs | Tue Dec 20 19:51:46 2016 UTC |
| 2 | Wed Feb 1 19:56:34 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 2145 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 2 | // This f ile is par t of the M icrosoft D ynamics CR M SDK code samples. | |||||
| 3 | // | |||||
| 4 | // Copyri ght (C) Mi crosoft Co rporation. All righ ts reserve d. | |||||
| 5 | // | |||||
| 6 | // This s ource code is intend ed only as a supplem ent to Mic rosoft | |||||
| 7 | // Develo pment Tool s and/or o n-line doc umentation . See the se other | |||||
| 8 | // materi als for de tailed inf ormation r egarding M icrosoft c ode sample s. | |||||
| 9 | // | |||||
| 10 | // THIS C ODE AND IN FORMATION ARE PROVID ED "AS IS" WITHOUT W ARRANTY OF ANY | |||||
| 11 | // KIND, EITHER EXP RESSED OR IMPLIED, I NCLUDING B UT NOT LIM ITED TO TH E | |||||
| 12 | // IMPLIE D WARRANTI ES OF MERC HANTABILIT Y AND/OR F ITNESS FOR A | |||||
| 13 | // PARTIC ULAR PURPO SE. | |||||
| 14 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 15 | ||||||
| 16 | //<snippet LINQExampl es> | |||||
| 17 | using Syst em; | |||||
| 18 | using Syst em.Service Model; | |||||
| 19 | using Syst em.Service Model.Desc ription; | |||||
| 20 | ||||||
| 21 | // These n amespaces are found in the Mic rosoft.Xrm .Sdk.dll a ssembly | |||||
| 22 | // located in the SD K\bin fold er of the SDK downlo ad. | |||||
| 23 | using Micr osoft.Xrm. Sdk; | |||||
| 24 | using Micr osoft.Xrm. Sdk.Query; | |||||
| 25 | using Micr osoft.Xrm. Sdk.Client ; | |||||
| 26 | using Micr osoft.Xrm. Sdk.Messag es; | |||||
| 27 | ||||||
| 28 | // This na mespace is found in Microsoft. Crm.Sdk.Pr oxy.dll as sembly | |||||
| 29 | // found i n the SDK\ bin folder . | |||||
| 30 | using Micr osoft.Crm. Sdk.Messag es; | |||||
| 31 | ||||||
| 32 | using Syst em.Linq; //names pace for L INQ querie s | |||||
| 33 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 34 | { | |||||
| 35 | /// <summ ary> | |||||
| 36 | /// Demon strates ba sic and co mplex LINQ queries u sed with M icrosoft D ynamics CR M | |||||
| 37 | /// </sum mary> | |||||
| 38 | /// <rema rks> | |||||
| 39 | /// At ru n-time, yo u will be given the option to delete all the | |||||
| 40 | /// datab ase record s created by this pr ogram.</re marks> | |||||
| 41 | public cl ass LINQ10 1 | |||||
| 42 | { | |||||
| 43 | #region Class Leve l Members | |||||
| 44 | private Guid _cont actId1; | |||||
| 45 | private Guid _cont actId2; | |||||
| 46 | private Guid _cont actId3; | |||||
| 47 | private Guid _cont actId4; | |||||
| 48 | private Guid _acco untId1; | |||||
| 49 | private Guid _acco untId2; | |||||
| 50 | private Guid _inci dentId1; | |||||
| 51 | private Guid _inci dentId2; | |||||
| 52 | private Guid _lead Id; | |||||
| 53 | ||||||
| 54 | private Organizati onServiceP roxy _serv iceProxy; | |||||
| 55 | ||||||
| 56 | #endregi on Class L evel Membe rs | |||||
| 57 | ||||||
| 58 | #region How To Sam ple Code | |||||
| 59 | /// <sum mary> | |||||
| 60 | /// This method fi rst connec ts to the Organizati on service . Afterwar ds, | |||||
| 61 | /// a se ries of LI NQ queries are perfo rmed. | |||||
| 62 | /// </su mmary> | |||||
| 63 | /// <par am name="s erverConfi g">Contain s server c onnection informatio n.</param> | |||||
| 64 | /// <par am name="p romptforDe lete">When True, the user will be prompt ed to dele te all | |||||
| 65 | /// crea ted entiti es.</param > | |||||
| 66 | public v oid Run(Se rverConnec tion.Confi guration s erverConfi g, bool pr omptforDel ete) | |||||
| 67 | { | |||||
| 68 | try | |||||
| 69 | { | |||||
| 70 | // Con nect to th e Organiza tion servi ce. | |||||
| 71 | // The using sta tement ass ures that the servic e proxy wi ll be prop erly dispo sed. | |||||
| 72 | using (_serviceP roxy = new Organizat ionService Proxy(serv erConfig.O rganizatio nUri, serv erConfig.H omeRealmUr i,serverCo nfig.Crede ntials, se rverConfig .DeviceCre dentials)) | |||||
| 73 | { | |||||
| 74 | // Th is stateme nt is requ ired to en able early -bound typ e support. | |||||
| 75 | _serv iceProxy.E nableProxy Types(); | |||||
| 76 | ||||||
| 77 | Creat eRequiredR ecords(); | |||||
| 78 | ||||||
| 79 | #regi on EarlyBo undExample s | |||||
| 80 | ||||||
| 81 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 82 | // LNQ Si mple where clause 1 | |||||
| 83 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 84 | ||||||
| 85 | Syste m.Console. WriteLine( ); | |||||
| 86 | Syste m.Console. WriteLine( "List of A ccounts us ing one wh ere clause "); | |||||
| 87 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 88 | ||||||
| 89 | //<sn ippetLINQE xamples1> | |||||
| 90 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 91 | { | |||||
| 92 | var query_wher e1 = from a in svcCo ntext.Acco untSet | |||||
| 93 | where a.Name.Co ntains("Co ntoso") | |||||
| 94 | selec t a; | |||||
| 95 | fore ach (var a in query_ where1) | |||||
| 96 | { | |||||
| 97 | Sys tem.Consol e.WriteLin e(a.Name + " " + a.A ddress1_Ci ty); | |||||
| 98 | } | |||||
| 99 | } | |||||
| 100 | //</s nippetLINQ Examples1> | |||||
| 101 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 102 | // OU TPUT: | |||||
| 103 | // Li st of Acco unts using where cla use 1 | |||||
| 104 | //=== ========== ========== ========== ===== | |||||
| 105 | //Con toso Ltd R edmond | |||||
| 106 | //=== ========== ========== ========== ==== | |||||
| 107 | Syste m.Console. WriteLine( ); | |||||
| 108 | ||||||
| 109 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 110 | // LNQ Si mple where clause 2 | |||||
| 111 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 112 | ||||||
| 113 | Syste m.Console. WriteLine( ); | |||||
| 114 | Syste m.Console. WriteLine( "List of A ccounts us ing two wh ere clause s"); | |||||
| 115 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 116 | //<sn ippetLINQE xamples2> | |||||
| 117 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 118 | { | |||||
| 119 | var query_wher e2 = from a in svcCo ntext.Acco untSet | |||||
| 120 | where a.Name.Co ntains("Co ntoso") | |||||
| 121 | where a.Address 1_City == "Redmond" | |||||
| 122 | selec t a; | |||||
| 123 | ||||||
| 124 | fore ach (var a in query_ where2) | |||||
| 125 | { | |||||
| 126 | Sys tem.Consol e.WriteLin e(a.Name + " " + a.A ddress1_Ci ty); | |||||
| 127 | } | |||||
| 128 | } | |||||
| 129 | //</s nippetLINQ Examples2> | |||||
| 130 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 131 | // OU TPUT: | |||||
| 132 | // List of Accounts u sing where clause 2 | |||||
| 133 | //=== ========== ========== ========== ===== | |||||
| 134 | //Con toso Ltd R edmond | |||||
| 135 | //=== ========== ========== ========== ==== | |||||
| 136 | Syste m.Console. WriteLine( ); | |||||
| 137 | ||||||
| 138 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 139 | // LNQ Jo in and sim ple where clause que ry | |||||
| 140 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 141 | ||||||
| 142 | Syste m.Console. WriteLine( ); | |||||
| 143 | Syste m.Console. WriteLine( "List of A ccount and Contact I nfo using where clau se"); | |||||
| 144 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 145 | //<sn ippetLINQE xamples3> | |||||
| 146 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 147 | { | |||||
| 148 | var query_wher e3 = from c in svcCo ntext.Cont actSet | |||||
| 149 | join a in svcCo ntext.Acco untSet | |||||
| 150 | on c. ContactId equals a.P rimaryCont actId.Id | |||||
| 151 | where a.Name.Co ntains("Co ntoso") | |||||
| 152 | where c.LastNam e.Contains ("Smith") | |||||
| 153 | selec t new | |||||
| 154 | { | |||||
| 155 | acco unt_name = a.Name, | |||||
| 156 | cont act_name = c.LastNam e | |||||
| 157 | }; | |||||
| 158 | ||||||
| 159 | fore ach (var c in query_ where3) | |||||
| 160 | { | |||||
| 161 | Sys tem.Consol e.WriteLin e("acct: " + | |||||
| 162 | c. account_na me + | |||||
| 163 | "\ t\t\t" + | |||||
| 164 | "c ontact: " + | |||||
| 165 | c. contact_na me); | |||||
| 166 | } | |||||
| 167 | } | |||||
| 168 | //</s nippetLINQ Examples3> | |||||
| 169 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 170 | // OU TPUT: | |||||
| 171 | //Lis t of Accou nt and Con tact Info using wher e clause | |||||
| 172 | //=== ========== ========== ========== ===== | |||||
| 173 | //acc t: Contoso Ltd con tact: Smit h | |||||
| 174 | //=== ========== ========== ========== ==== | |||||
| 175 | Syste m.Console. WriteLine( ); | |||||
| 176 | ||||||
| 177 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 178 | // LNQ Using th e Distinct operator (returns o nly one of duplicate elements) | |||||
| 179 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 180 | ||||||
| 181 | Syste m.Console. WriteLine( ); | |||||
| 182 | Syste m.Console. WriteLine( "List of C ontact Inf o using Di stinct ope rator"); | |||||
| 183 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 184 | ||||||
| 185 | //<sn ippetLINQE xamples4> | |||||
| 186 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 187 | { | |||||
| 188 | var query_dist inct = (fr om c in sv cContext.C ontactSet | |||||
| 189 | se lect c.Las tName).Dis tinct(); | |||||
| 190 | fore ach (var c in query_ distinct) | |||||
| 191 | { | |||||
| 192 | Sys tem.Consol e.WriteLin e(c); | |||||
| 193 | } | |||||
| 194 | } | |||||
| 195 | //</s nippetLINQ Examples4> | |||||
| 196 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 197 | // OU TPUT: | |||||
| 198 | // List of Contact In fo using D istinct op erator | |||||
| 199 | //=== ========== ========== ========== ===== | |||||
| 200 | //Par ker | |||||
| 201 | //Smi th | |||||
| 202 | //Wil cox | |||||
| 203 | //=== ========== ========== ========== ==== | |||||
| 204 | Syste m.Console. WriteLine( ); | |||||
| 205 | ||||||
| 206 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 207 | // LNQ Si mple inner join 1 | |||||
| 208 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 209 | ||||||
| 210 | Syste m.Console. WriteLine( ); | |||||
| 211 | Syste m.Console. WriteLine( "List of C ontact and Account I nfo Using join 1 "); | |||||
| 212 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 213 | //<sn ippetLINQE xamples5> | |||||
| 214 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 215 | { | |||||
| 216 | var query_join 1 = from c in svcCon text.Conta ctSet | |||||
| 217 | join a in svcCon text.Accou ntSet | |||||
| 218 | on c.Co ntactId eq uals a.Pri maryContac tId.Id | |||||
| 219 | select new | |||||
| 220 | { | |||||
| 221 | c.Ful lName, | |||||
| 222 | c.Add ress1_City , | |||||
| 223 | a.Nam e, | |||||
| 224 | a.Add ress1_Name | |||||
| 225 | }; | |||||
| 226 | fore ach (var c in query_ join1) | |||||
| 227 | { | |||||
| 228 | Sys tem.Consol e.WriteLin e("acct: " + | |||||
| 229 | c. Name + | |||||
| 230 | "\ t\t\t" + | |||||
| 231 | "c ontact: " + | |||||
| 232 | c. FullName); | |||||
| 233 | } | |||||
| 234 | } | |||||
| 235 | //</s nippetLINQ Examples5> | |||||
| 236 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 237 | // OU TPUT: | |||||
| 238 | //Lis t of Conta ct and Acc ount Info Using join 1 | |||||
| 239 | //=== ========== ========== ========== = | |||||
| 240 | //acc t: Contoso Ltd con tact: Bria n Smith | |||||
| 241 | //=== ========== ========== ========== ==== | |||||
| 242 | Syste m.Console. WriteLine( ); | |||||
| 243 | ||||||
| 244 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 245 | // LNQ Multiple join 4 | |||||
| 246 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 247 | ||||||
| 248 | Syste m.Console. WriteLine( ); | |||||
| 249 | Syste m.Console. WriteLine( "List of C ontact, Ac count, Lea d Info usi ng multipl e join 4") ; | |||||
| 250 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 251 | //<sn ippetLINQE xamples8> | |||||
| 252 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 253 | { | |||||
| 254 | var query_join 4 = from a in svcCon text.Accou ntSet | |||||
| 255 | join c in svcCon text.Conta ctSet | |||||
| 256 | on a.P rimaryCont actId.Id e quals c.Co ntactId | |||||
| 257 | join l in svcCon text.LeadS et | |||||
| 258 | on a.O riginating LeadId.Id equals l.L eadId | |||||
| 259 | select new | |||||
| 260 | { | |||||
| 261 | conta ct_name = c.FullName , | |||||
| 262 | accou nt_name = a.Name, | |||||
| 263 | lead_ name = l.F ullName | |||||
| 264 | }; | |||||
| 265 | fore ach (var c in query_ join4) | |||||
| 266 | { | |||||
| 267 | Sys tem.Consol e.WriteLin e(c.contac t_name + | |||||
| 268 | " " + | |||||
| 269 | c. account_na me + | |||||
| 270 | " " + | |||||
| 271 | c. lead_name) ; | |||||
| 272 | } | |||||
| 273 | } | |||||
| 274 | //</s nippetLINQ Examples8> | |||||
| 275 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 276 | // OU TPUT: | |||||
| 277 | //Lis t of Conta ct, Accoun t, Lead In fo using m ultiple jo in 4 | |||||
| 278 | //=== ========== ========== ========== = | |||||
| 279 | //Bri an Smith Contoso Lt d Diogo A ndrade | |||||
| 280 | //=== ========== ========== ========== = | |||||
| 281 | Syste m.Console. WriteLine( ); | |||||
| 282 | ||||||
| 283 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 284 | // LNQ Self joi n 5 | |||||
| 285 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 286 | Syste m.Console. WriteLine( ); | |||||
| 287 | Syste m.Console. WriteLine( "List of A ccount Inf o using se lf join 5" ); | |||||
| 288 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 289 | //<sn ippetLINQE xamples9> | |||||
| 290 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 291 | { | |||||
| 292 | var query_join 5 = from a in svcCon text.Accou ntSet | |||||
| 293 | join a 2 in svcCo ntext.Acco untSet | |||||
| 294 | on a.P arentAccou ntId.Id eq uals a2.Ac countId | |||||
| 295 | ||||||
| 296 | select new | |||||
| 297 | { | |||||
| 298 | accou nt_name = a.Name, | |||||
| 299 | accou nt_city = a.Address1 _City | |||||
| 300 | }; | |||||
| 301 | fore ach (var c in query_ join5) | |||||
| 302 | { | |||||
| 303 | Sys tem.Consol e.WriteLin e(c.accoun t_name + " " + c.ac count_city ); | |||||
| 304 | } | |||||
| 305 | } | |||||
| 306 | //</s nippetLINQ Examples9> | |||||
| 307 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 308 | // OU TPUT: | |||||
| 309 | //Lis t of Accou nt Info us ing self j oin 5 | |||||
| 310 | //=== ========== ========== ========== = | |||||
| 311 | //Con toso Ltd Redmond | |||||
| 312 | //=== ========== ========== ========== = | |||||
| 313 | Syste m.Console. WriteLine( ); | |||||
| 314 | ||||||
| 315 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 316 | // LNQ Double j oin 6 | |||||
| 317 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 318 | ||||||
| 319 | Syste m.Console. WriteLine( ); | |||||
| 320 | Syste m.Console. WriteLine( "List of C ontact Inf o using do uble join 6"); | |||||
| 321 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 322 | //<sn ippetLINQE xamples10> | |||||
| 323 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 324 | { | |||||
| 325 | var query_join 6 = from c in svcCon text.Conta ctSet | |||||
| 326 | join a in svcCon text.Accou ntSet | |||||
| 327 | on c.C ontactId e quals a.Pr imaryConta ctId.Id | |||||
| 328 | join a 2 in svcCo ntext.Acco untSet | |||||
| 329 | on a.P arentAccou ntId.Id eq uals a2.Ac countId | |||||
| 330 | select new | |||||
| 331 | { | |||||
| 332 | conta ct_name = c.FullName , | |||||
| 333 | accou nt_name = a.Name | |||||
| 334 | }; | |||||
| 335 | fore ach (var c in query_ join6) | |||||
| 336 | { | |||||
| 337 | Sys tem.Consol e.WriteLin e(c.contac t_name + " " + c.ac count_name ); | |||||
| 338 | } | |||||
| 339 | } | |||||
| 340 | //</s nippetLINQ Examples10 > | |||||
| 341 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 342 | // OU TPUT: | |||||
| 343 | //Lis t of Conta ct Info us ing double join 6 | |||||
| 344 | //=== ========== ========== ========== = | |||||
| 345 | //Bri an Smith Contoso Lt d | |||||
| 346 | //=== ========== ========== ========== = | |||||
| 347 | Syste m.Console. WriteLine( ); | |||||
| 348 | ||||||
| 349 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 350 | // LNQ Entity F ields join 7 | |||||
| 351 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 352 | Syste m.Console. WriteLine( ); | |||||
| 353 | Syste m.Console. WriteLine( "List of C ontact Inf o using en tity field join 7"); | |||||
| 354 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 355 | //<sn ippetLINQE xamples11> | |||||
| 356 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 357 | { | |||||
| 358 | var list_join = (from a in svcCont ext.Accoun tSet | |||||
| 359 | join c in svcCont ext.Contac tSet | |||||
| 360 | on a.Pr imaryConta ctId.Id eq uals c.Con tactId | |||||
| 361 | where a .Name == " Contoso Lt d" && | |||||
| 362 | a.Addre ss1_Name = = "Contoso Pharmaceu ticals" | |||||
| 363 | select a).ToList( ); | |||||
| 364 | fore ach (var c in list_j oin) | |||||
| 365 | { | |||||
| 366 | Sys tem.Consol e.WriteLin e("Account " + list_ join[0].Na me | |||||
| 367 | + " and i t's primar y contact " | |||||
| 368 | + list_jo in[0].Prim aryContact Id.Id); | |||||
| 369 | } | |||||
| 370 | } | |||||
| 371 | //</s nippetLINQ Examples11 > | |||||
| 372 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 373 | // OU TPUT: | |||||
| 374 | // Ac count Cont oso Ltd an d it's pri mary conta ct 918228a f-04a1-e01 1-b1b7-001 55dba3818 | |||||
| 375 | //=== ========== ========== ========== = | |||||
| 376 | Syste m.Console. WriteLine( ); | |||||
| 377 | ||||||
| 378 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 379 | // LNQ Left joi n 8 | |||||
| 380 | // A left join is design ed to retu rn parents with and without ch ildren fro m two sour ces. | |||||
| 381 | // There is a correlati on between parent an d child, b ut no chil d may actu ally exist . | |||||
| 382 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 383 | Syste m.Console. WriteLine( ); | |||||
| 384 | Syste m.Console. WriteLine( "List of C ontact Inf o using le ft join 8" ); | |||||
| 385 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 386 | //<sn ippetLINQE xamples12> | |||||
| 387 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 388 | { | |||||
| 389 | var query_join 8 = from a in svcCon text.Accou ntSet | |||||
| 390 | join c in svcCon text.Conta ctSet | |||||
| 391 | on a.P rimaryCont actId.Id e quals c.Co ntactId | |||||
| 392 | into g r | |||||
| 393 | from c _joined in gr.Defaul tIfEmpty() | |||||
| 394 | select new | |||||
| 395 | { | |||||
| 396 | conta ct_name = c_joined.F ullName, | |||||
| 397 | accou nt_name = a.Name | |||||
| 398 | }; | |||||
| 399 | fore ach (var c in query_ join8) | |||||
| 400 | { | |||||
| 401 | Sys tem.Consol e.WriteLin e(c.contac t_name + " " + c.ac count_name ); | |||||
| 402 | } | |||||
| 403 | } | |||||
| 404 | //</s nippetLINQ Examples12 > | |||||
| 405 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 406 | // OU TPUT: | |||||
| 407 | //Lis t of Conta ct Info us ing left j oin 8 | |||||
| 408 | //=== ========== ========== ========== = | |||||
| 409 | // C oho Winery | |||||
| 410 | //Bri an Smith Contoso Lt d | |||||
| 411 | //=== ========== ========== ========== = | |||||
| 412 | Syste m.Console. WriteLine( ); | |||||
| 413 | ||||||
| 414 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 415 | // LNQ Using th e Equals o perator 1 | |||||
| 416 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 417 | ||||||
| 418 | Syste m.Console. WriteLine( ); | |||||
| 419 | Syste m.Console. WriteLine( "List of C ontact Inf o using Eq uals opera tor 1"); | |||||
| 420 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 421 | //<sn ippetLINQE xamples14> | |||||
| 422 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 423 | { | |||||
| 424 | var query_equa ls1 = from c in svcC ontext.Con tactSet | |||||
| 425 | wher e c.FirstN ame.Equals ("Colin") | |||||
| 426 | sele ct new | |||||
| 427 | { | |||||
| 428 | c.F irstName, | |||||
| 429 | c.L astName, | |||||
| 430 | c.A ddress1_Ci ty | |||||
| 431 | }; | |||||
| 432 | fore ach (var c in query_ equals1) | |||||
| 433 | { | |||||
| 434 | Sys tem.Consol e.WriteLin e(c.FirstN ame + | |||||
| 435 | " " + c.Last Name + | |||||
| 436 | " " + c.Addr ess1_City) ; | |||||
| 437 | } | |||||
| 438 | } | |||||
| 439 | //</s nippetLINQ Examples14 > | |||||
| 440 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 441 | // OU TPUT: | |||||
| 442 | // Li st of Cont act Info u sing Equal s operator 1 | |||||
| 443 | //=== ========== ========== ========== ===== | |||||
| 444 | //Col in Wilcox Redmond | |||||
| 445 | //=== ========== ========== ========== ==== | |||||
| 446 | Syste m.Console. WriteLine( ); | |||||
| 447 | ||||||
| 448 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 449 | // LNQ Using th e Equals o perator 2 | |||||
| 450 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 451 | ||||||
| 452 | Syste m.Console. WriteLine( ); | |||||
| 453 | Syste m.Console. WriteLine( "List of C ontact Inf o using Eq uals opera tor 2"); | |||||
| 454 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 455 | //<sn ippetLINQE xamples15> | |||||
| 456 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 457 | { | |||||
| 458 | var query_equa ls2 = from c in svcC ontext.Con tactSet | |||||
| 459 | wher e c.Family StatusCode .Equals(3) | |||||
| 460 | sele ct new | |||||
| 461 | { | |||||
| 462 | c.F irstName, | |||||
| 463 | c.L astName, | |||||
| 464 | c.A ddress1_Ci ty | |||||
| 465 | }; | |||||
| 466 | fore ach (var c in query_ equals2) | |||||
| 467 | { | |||||
| 468 | Sys tem.Consol e.WriteLin e(c.FirstN ame + | |||||
| 469 | " " + c.Last Name + | |||||
| 470 | " " + c.Addr ess1_City) ; | |||||
| 471 | } | |||||
| 472 | } | |||||
| 473 | //</s nippetLINQ Examples15 > | |||||
| 474 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 475 | // OU TPUT: | |||||
| 476 | // Li st of Cont act Info u sing Equal s operator 2 | |||||
| 477 | //=== ========== ========== ========== ===== | |||||
| 478 | //Bri an Smith B ellevue | |||||
| 479 | //Dar ren Parker Kirkland | |||||
| 480 | //Ben Smith Kir kland | |||||
| 481 | //=== ========== ========== ========== ==== | |||||
| 482 | Syste m.Console. WriteLine( ); | |||||
| 483 | ||||||
| 484 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 485 | // LNQ Using th e not equa ls operato r 1 | |||||
| 486 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 487 | ||||||
| 488 | Syste m.Console. WriteLine( ); | |||||
| 489 | Syste m.Console. WriteLine( "List of C ontact Inf o using no t equals o perator 1" ); | |||||
| 490 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 491 | //<sn ippetLINQE xamples16> | |||||
| 492 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 493 | { | |||||
| 494 | var query_ne1 = from c i n svcConte xt.Contact Set | |||||
| 495 | where c. Address1_C ity != "Re dmond" | |||||
| 496 | select n ew | |||||
| 497 | { | |||||
| 498 | c.First Name, | |||||
| 499 | c.LastN ame, | |||||
| 500 | c.Addre ss1_City | |||||
| 501 | }; | |||||
| 502 | fore ach (var c in query_ ne1) | |||||
| 503 | { | |||||
| 504 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + | |||||
| 505 | c. LastName + " " + c.A ddress1_Ci ty); | |||||
| 506 | } | |||||
| 507 | } | |||||
| 508 | //</s nippetLINQ Examples16 > | |||||
| 509 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 510 | // OU TPUT: | |||||
| 511 | // Li st of Cont act Info u sing not e quals oper ator 1 | |||||
| 512 | //=== ========== ========== ========== ===== | |||||
| 513 | //Bri an Smith B ellevue | |||||
| 514 | //Dar ren Parker Kirkland | |||||
| 515 | //Ben Smith Kir kland | |||||
| 516 | //=== ========== ========== ========== ==== | |||||
| 517 | Syste m.Console. WriteLine( ); | |||||
| 518 | ||||||
| 519 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 520 | // LNQ Using th e not equa ls operato r 2 | |||||
| 521 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 522 | ||||||
| 523 | Syste m.Console. WriteLine( ); | |||||
| 524 | Syste m.Console. WriteLine( "List of C ontact Inf o using no t equals o perator 2" ); | |||||
| 525 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 526 | //<sn ippetLINQE xamples17> | |||||
| 527 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 528 | { | |||||
| 529 | var query_ne2 = from c i n svcConte xt.Contact Set | |||||
| 530 | where !c .FirstName .Equals("C olin") | |||||
| 531 | select n ew | |||||
| 532 | { | |||||
| 533 | c.First Name, | |||||
| 534 | c.LastN ame, | |||||
| 535 | c.Addre ss1_City | |||||
| 536 | }; | |||||
| 537 | ||||||
| 538 | fore ach (var c in query_ ne2) | |||||
| 539 | { | |||||
| 540 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + | |||||
| 541 | c. LastName + " " + c.A ddress1_Ci ty); | |||||
| 542 | } | |||||
| 543 | } | |||||
| 544 | //</s nippetLINQ Examples17 > | |||||
| 545 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 546 | // OU TPUT: | |||||
| 547 | // List of Contact In fo using n ot equals operator 2 | |||||
| 548 | //=== ========== ========== ========== ===== | |||||
| 549 | //Bri an Smith B ellevue | |||||
| 550 | //Dar ren Parker Kirkland | |||||
| 551 | //Ben Smith Kir kland | |||||
| 552 | //=== ========== ========== ========== ==== | |||||
| 553 | Syste m.Console. WriteLine( ); | |||||
| 554 | ||||||
| 555 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 556 | // LINQ Method-ba sed LINQ q uery with where clau se | |||||
| 557 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 558 | //<sn ippetLINQE xamples18> | |||||
| 559 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 560 | { | |||||
| 561 | var methodResu lts = svcC ontext.Con tactSet | |||||
| 562 | .Wh ere(a => a .LastName == "Smith" ); | |||||
| 563 | var methodResu lts2 = svc Context.Co ntactSet | |||||
| 564 | .Wh ere(a => a .LastName. StartsWith ("Smi")); | |||||
| 565 | Cons ole.WriteL ine(); | |||||
| 566 | Cons ole.WriteL ine("Metho d query us ing Lambda expressio n"); | |||||
| 567 | Cons ole.WriteL ine("----- ---------- ---------- ---------- ----"); | |||||
| 568 | fore ach (var a in method Results) | |||||
| 569 | { | |||||
| 570 | Con sole.Write Line("Name : " + a.Fi rstName + " " + a.La stName); | |||||
| 571 | } | |||||
| 572 | Cons ole.WriteL ine("----- ---------- ---------- ---------- ----"); | |||||
| 573 | Cons ole.WriteL ine("Metho d query 2 using Lamb da express ion"); | |||||
| 574 | Cons ole.WriteL ine("----- ---------- ---------- ---------- ----"); | |||||
| 575 | fore ach (var a in method Results2) | |||||
| 576 | { | |||||
| 577 | Con sole.Write Line("Name : " + a.At tributes[" firstname" ] + | |||||
| 578 | " " + a.Attr ibutes["la stname"]); | |||||
| 579 | } | |||||
| 580 | } | |||||
| 581 | //</s nippetLINQ Examples18 > | |||||
| 582 | Conso le.WriteLi ne("------ ---------- ---------- ---------- ---"); | |||||
| 583 | // OU TPUT: | |||||
| 584 | //Met hod query using Lamb da express ion | |||||
| 585 | //--- ---------- ---------- ---------- ------ | |||||
| 586 | //Nam e: Brian S mith | |||||
| 587 | //Nam e: Ben Smi th | |||||
| 588 | //--- ---------- ---------- ---------- ------ | |||||
| 589 | //Met hod query 2 using La mbda expre ssion | |||||
| 590 | //--- ---------- ---------- ---------- ------ | |||||
| 591 | //Nam e: Brian S mith | |||||
| 592 | //Nam e: Ben Smi th | |||||
| 593 | //--- ---------- ---------- ---------- ------ | |||||
| 594 | ||||||
| 595 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 596 | // LNQ Using th e greater than opera tor 1 | |||||
| 597 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 598 | ||||||
| 599 | Syste m.Console. WriteLine( ); | |||||
| 600 | Syste m.Console. WriteLine( "List of C ontact Inf o using gr eater than operator 1"); | |||||
| 601 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 602 | //<sn ippetLINQE xamples20> | |||||
| 603 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 604 | { | |||||
| 605 | var query_gt1 = from c i n svcConte xt.Contact Set | |||||
| 606 | where c. Anniversar y > new Da teTime(201 0, 2, 5) | |||||
| 607 | select n ew | |||||
| 608 | { | |||||
| 609 | c.First Name, | |||||
| 610 | c.LastN ame, | |||||
| 611 | c.Addre ss1_City | |||||
| 612 | }; | |||||
| 613 | ||||||
| 614 | fore ach (var c in query_ gt1) | |||||
| 615 | { | |||||
| 616 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + | |||||
| 617 | c. LastName + " " + c.A ddress1_Ci ty); | |||||
| 618 | } | |||||
| 619 | } | |||||
| 620 | //</s nippetLINQ Examples20 > | |||||
| 621 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 622 | // OU TPUT: | |||||
| 623 | //Lis t of Conta ct Info us ing greate r than ope rator 1 | |||||
| 624 | //=== ========== ========== ========== ===== | |||||
| 625 | //Col in Wilcox Redmond | |||||
| 626 | //Bri an Smith B ellevue | |||||
| 627 | //Dar ren Parker Kirkland | |||||
| 628 | //Ben Smith Kir kland | |||||
| 629 | //=== ========== ========== ========== ==== | |||||
| 630 | Syste m.Console. WriteLine( ); | |||||
| 631 | ||||||
| 632 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 633 | // LNQ Using th e greater than opera tor 2 | |||||
| 634 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 635 | ||||||
| 636 | Syste m.Console. WriteLine( ); | |||||
| 637 | Syste m.Console. WriteLine( "List of C ontact Inf o using gr eater than operator 2"); | |||||
| 638 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 639 | //<sn ippetLINQE xamples21> | |||||
| 640 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 641 | { | |||||
| 642 | var query_gt2 = from c i n svcConte xt.Contact Set | |||||
| 643 | where c. CreditLimi t.Value > 20000 | |||||
| 644 | select n ew | |||||
| 645 | { | |||||
| 646 | c.First Name, | |||||
| 647 | c.LastN ame, | |||||
| 648 | c.Addre ss1_City | |||||
| 649 | }; | |||||
| 650 | fore ach (var c in query_ gt2) | |||||
| 651 | { | |||||
| 652 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + | |||||
| 653 | c. LastName + " " + c.A ddress1_Ci ty); | |||||
| 654 | } | |||||
| 655 | } | |||||
| 656 | //</s nippetLINQ Examples21 > | |||||
| 657 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 658 | // OU TPUT: | |||||
| 659 | // List of Contact In fo using g reater tha n operator 2 | |||||
| 660 | //=== ========== ========== ========== ===== | |||||
| 661 | //Bri an Smith B ellevue | |||||
| 662 | //=== ========== ========== ========== ==== | |||||
| 663 | Syste m.Console. WriteLine( ); | |||||
| 664 | ||||||
| 665 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 666 | // LNQ Using th e ge and l e operator s 1 | |||||
| 667 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 668 | ||||||
| 669 | Syste m.Console. WriteLine( ); | |||||
| 670 | Syste m.Console. WriteLine( "List of C ontact Inf o using th e ge and l e operator s 1"); | |||||
| 671 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 672 | //<sn ippetLINQE xamples22> | |||||
| 673 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 674 | { | |||||
| 675 | var query_gele 1 = from c in svcCon text.Conta ctSet | |||||
| 676 | where c.CreditLi mit.Value >= 200 && | |||||
| 677 | c.Cred itLimit.Va lue <= 400 | |||||
| 678 | select new | |||||
| 679 | { | |||||
| 680 | c.Fir stName, | |||||
| 681 | c.Las tName | |||||
| 682 | }; | |||||
| 683 | fore ach (var c in query_ gele1) | |||||
| 684 | { | |||||
| 685 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + c.LastNa me); | |||||
| 686 | } | |||||
| 687 | } | |||||
| 688 | //</s nippetLINQ Examples22 > | |||||
| 689 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 690 | // OU TPUT: | |||||
| 691 | // List of Contact In fo using t he ge and le operato rs 1 | |||||
| 692 | //=== ========== ========== ========== ===== | |||||
| 693 | //Col in Wilcox | |||||
| 694 | //=== ========== ========== ========== ==== | |||||
| 695 | Syste m.Console. WriteLine( ); | |||||
| 696 | ||||||
| 697 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 698 | // LNQ Using th e contains operator 1 | |||||
| 699 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 700 | ||||||
| 701 | Syste m.Console. WriteLine( ); | |||||
| 702 | Syste m.Console. WriteLine( "List of C ontact Inf o using th e contains operator 1"); | |||||
| 703 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 704 | //<sn ippetLINQE xamples23> | |||||
| 705 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 706 | { | |||||
| 707 | var query_cont ains1 = fr om c in sv cContext.C ontactSet | |||||
| 708 | wh ere c.Desc ription.Co ntains("Al pine") | |||||
| 709 | se lect new | |||||
| 710 | { | |||||
| 711 | c .FirstName , | |||||
| 712 | c .LastName | |||||
| 713 | }; | |||||
| 714 | fore ach (var c in query_ contains1) | |||||
| 715 | { | |||||
| 716 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + c.LastNa me); | |||||
| 717 | } | |||||
| 718 | } | |||||
| 719 | //</s nippetLINQ Examples23 > | |||||
| 720 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 721 | // OU TPUT: | |||||
| 722 | //Lis t of Conta ct Info us ing the co ntains ope rator 1 | |||||
| 723 | //=== ========== ========== ========== ===== | |||||
| 724 | //Col in Wilcox | |||||
| 725 | //=== ========== ========== ========== ==== | |||||
| 726 | Syste m.Console. WriteLine( ); | |||||
| 727 | ||||||
| 728 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 729 | // LNQ Using th e negated contains o perator 2 | |||||
| 730 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 731 | ||||||
| 732 | Syste m.Console. WriteLine( ); | |||||
| 733 | Syste m.Console. WriteLine( "List of C ontact Inf o using th e negated contains o perator 2" ); | |||||
| 734 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 735 | //<sn ippetLINQE xamples24> | |||||
| 736 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 737 | { | |||||
| 738 | var query_cont ains2 = fr om c in sv cContext.C ontactSet | |||||
| 739 | wh ere !c.Des cription.C ontains("C oho") | |||||
| 740 | se lect new | |||||
| 741 | { | |||||
| 742 | c .FirstName , | |||||
| 743 | c .LastName | |||||
| 744 | }; | |||||
| 745 | fore ach (var c in query_ contains2) | |||||
| 746 | { | |||||
| 747 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + c.LastNa me); | |||||
| 748 | } | |||||
| 749 | } | |||||
| 750 | //</s nippetLINQ Examples24 > | |||||
| 751 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 752 | // OU TPUT: | |||||
| 753 | //Lis t of Conta ct Info us ing the ne gated cont ains opera tor 2 | |||||
| 754 | //=== ========== ========== ========== ===== | |||||
| 755 | //Col in Wilcox | |||||
| 756 | //=== ========== ========== ========== ==== | |||||
| 757 | Syste m.Console. WriteLine( ); | |||||
| 758 | ||||||
| 759 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 760 | // LNQ Using th e StartsWi th operato r 1 | |||||
| 761 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 762 | ||||||
| 763 | Syste m.Console. WriteLine( ); | |||||
| 764 | Syste m.Console. WriteLine( "List of C ontact Inf o using th e StartsWi th operato r 1"); | |||||
| 765 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 766 | //<sn ippetLINQE xamples26> | |||||
| 767 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 768 | { | |||||
| 769 | var query_star tswith1 = from c in svcContext .ContactSe t | |||||
| 770 | where c.Fi rstName.St artsWith(" Bri") | |||||
| 771 | select new | |||||
| 772 | { | |||||
| 773 | c.FirstNa me, | |||||
| 774 | c.LastNam e | |||||
| 775 | }; | |||||
| 776 | fore ach (var c in query_ startswith 1) | |||||
| 777 | { | |||||
| 778 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + c.LastNa me); | |||||
| 779 | } | |||||
| 780 | } | |||||
| 781 | //</s nippetLINQ Examples26 > | |||||
| 782 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 783 | // OU TPUT: | |||||
| 784 | //Lis t of Conta ct Info us ing the St artsWith o perator 1 | |||||
| 785 | //=== ========== ========== ========== ===== | |||||
| 786 | //Bri an Smith | |||||
| 787 | //=== ========== ========== ========== ==== | |||||
| 788 | Syste m.Console. WriteLine( ); | |||||
| 789 | ||||||
| 790 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 791 | // LNQ Using th e endswith operator 1 | |||||
| 792 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 793 | ||||||
| 794 | Syste m.Console. WriteLine( ); | |||||
| 795 | Syste m.Console. WriteLine( "List of C ontact Inf o using th e EndsWith operator 1"); | |||||
| 796 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 797 | //<sn ippetLINQE xamples27> | |||||
| 798 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 799 | { | |||||
| 800 | var query_ends with1 = fr om c in sv cContext.C ontactSet | |||||
| 801 | wh ere c.Last Name.EndsW ith("cox") | |||||
| 802 | se lect new | |||||
| 803 | { | |||||
| 804 | c .FirstName , | |||||
| 805 | c .LastName | |||||
| 806 | }; | |||||
| 807 | fore ach (var c in query_ endswith1) | |||||
| 808 | { | |||||
| 809 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + c.LastNa me); | |||||
| 810 | } | |||||
| 811 | } | |||||
| 812 | //</s nippetLINQ Examples27 > | |||||
| 813 | ||||||
| 814 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 815 | // OU TPUT: | |||||
| 816 | //Lis t of Conta ct Info us ing the En dsWith ope rator 1 | |||||
| 817 | //=== ========== ========== ========== ===== | |||||
| 818 | //Col in Wilcox | |||||
| 819 | //=== ========== ========== ========== ==== | |||||
| 820 | Syste m.Console. WriteLine( ); | |||||
| 821 | ||||||
| 822 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 823 | // LNQ Using th e && and | | operator s 1 | |||||
| 824 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 825 | ||||||
| 826 | Syste m.Console. WriteLine( ); | |||||
| 827 | Syste m.Console. WriteLine( "List of C ontact Inf o using th e && and | | operator s 1"); | |||||
| 828 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 829 | //<sn ippetLINQE xamples28> | |||||
| 830 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 831 | { | |||||
| 832 | var query_ando r1 = from c in svcCo ntext.Cont actSet | |||||
| 833 | where ((c.Addre ss1_City = = "Redmond " || | |||||
| 834 | c.Add ress1_City == "Belle vue") && | |||||
| 835 | (c.Cr editLimit. Value != n ull && | |||||
| 836 | c.Cre ditLimit.V alue >= 20 0)) | |||||
| 837 | selec t c; | |||||
| 838 | ||||||
| 839 | fore ach (var c in query_ andor1) | |||||
| 840 | { | |||||
| 841 | Sys tem.Consol e.WriteLin e(c.LastNa me + ", " + c.FirstN ame + " " + | |||||
| 842 | c. Address1_C ity + " " + c.Credit Limit.Valu e); | |||||
| 843 | } | |||||
| 844 | } | |||||
| 845 | //</s nippetLINQ Examples28 > | |||||
| 846 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 847 | // OU TPUT: | |||||
| 848 | // Li st of Cont act Info u sing the & & and || o perators 1 | |||||
| 849 | //=== ========== ========== ========== ===== | |||||
| 850 | //Wil cox, Colin Redmond 3 00.0000 | |||||
| 851 | //Smi th, Brian Bellevue 3 0000.0000 | |||||
| 852 | //=== ========== ========== ========== ==== | |||||
| 853 | Syste m.Console. WriteLine( ); | |||||
| 854 | ||||||
| 855 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 856 | // LNQ Using th e orderby operator 1 | |||||
| 857 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 858 | ||||||
| 859 | Syste m.Console. WriteLine( ); | |||||
| 860 | Syste m.Console. WriteLine( "List of C ontact Inf o using th e orderby operator 1 "); | |||||
| 861 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 862 | //<sn ippetLINQE xamples29> | |||||
| 863 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 864 | { | |||||
| 865 | var query_orde rby1 = fro m c in svc Context.Co ntactSet | |||||
| 866 | whe re !c.Cred itLimit.Eq uals(null) | |||||
| 867 | ord erby c.Cre ditLimit d escending | |||||
| 868 | sel ect new | |||||
| 869 | { | |||||
| 870 | li mit = c.Cr editLimit, | |||||
| 871 | fi rst = c.Fi rstName, | |||||
| 872 | la st = c.Las tName | |||||
| 873 | }; | |||||
| 874 | fore ach (var c in query_ orderby1) | |||||
| 875 | { | |||||
| 876 | Sys tem.Consol e.WriteLin e(c.limit. Value + " " + | |||||
| 877 | c. last + ", " + c.firs t); | |||||
| 878 | } | |||||
| 879 | } | |||||
| 880 | //</s nippetLINQ Examples29 > | |||||
| 881 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 882 | // OU TPUT: | |||||
| 883 | //Lis t of Conta ct Info us ing the or derby oper ator 1 | |||||
| 884 | //=== ========== ========== ========== ===== | |||||
| 885 | //300 00.0000 Sm ith, Brian | |||||
| 886 | //120 00.0000 Sm ith, Ben | |||||
| 887 | //100 00.0000 Pa rker, Darr en | |||||
| 888 | //300 .0000 Wilc ox, Colin | |||||
| 889 | //=== ========== ========== ========== ==== | |||||
| 890 | Syste m.Console. WriteLine( ); | |||||
| 891 | ||||||
| 892 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 893 | // LNQ Using th e orderby operator 2 | |||||
| 894 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 895 | ||||||
| 896 | Syste m.Console. WriteLine( ); | |||||
| 897 | Syste m.Console. WriteLine( "List of C ontact Inf o using th e orderby operator 2 "); | |||||
| 898 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 899 | //<sn ippetLINQE xamples30> | |||||
| 900 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 901 | { | |||||
| 902 | var query_orde rby2 = fro m c in svc Context.Co ntactSet | |||||
| 903 | ord erby c.Las tName desc ending, | |||||
| 904 | c.F irstName a scending | |||||
| 905 | sel ect new | |||||
| 906 | { | |||||
| 907 | fi rst = c.Fi rstName, | |||||
| 908 | la st = c.Las tName | |||||
| 909 | }; | |||||
| 910 | ||||||
| 911 | fore ach (var c in query_ orderby2) | |||||
| 912 | { | |||||
| 913 | Sys tem.Consol e.WriteLin e(c.last + ", " + c. first); | |||||
| 914 | } | |||||
| 915 | } | |||||
| 916 | //</s nippetLINQ Examples30 > | |||||
| 917 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 918 | // OU TPUT: | |||||
| 919 | //Lis t of Conta ct Info us ing the or derby oper ator 2 | |||||
| 920 | //=== ========== ========== ========== ===== | |||||
| 921 | //Wil cox, Colin | |||||
| 922 | //Smi th, Ben | |||||
| 923 | //Smi th, Brian | |||||
| 924 | //Par ker, Darre n | |||||
| 925 | //=== ========== ========== ========== ==== | |||||
| 926 | Syste m.Console. WriteLine( ); | |||||
| 927 | ||||||
| 928 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 929 | // LNQ Using th e First an d Single o perators | |||||
| 930 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 931 | ||||||
| 932 | Syste m.Console. WriteLine( ); | |||||
| 933 | Syste m.Console. WriteLine( "========= ========== ======="); | |||||
| 934 | //<sn ippetLINQE xamples31> | |||||
| 935 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 936 | { | |||||
| 937 | Cont act firstc ontact = s vcContext. ContactSet .First(); | |||||
| 938 | ||||||
| 939 | Cont act single contact = svcContext .ContactSe t.Single(c => c.Cont actId == _ contactId1 ); | |||||
| 940 | Syst em.Console .WriteLine (firstcont act.LastNa me + ", " + | |||||
| 941 | fir stcontact. FirstName + " is the first con tact"); | |||||
| 942 | Syst em.Console .WriteLine ("======== ========== ========") ; | |||||
| 943 | Syst em.Console .WriteLine (singlecon tact.LastN ame + ", " + | |||||
| 944 | sin glecontact .FirstName + " is th e single c ontact"); | |||||
| 945 | } | |||||
| 946 | //</s nippetLINQ Examples31 > | |||||
| 947 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 948 | // OU TPUT: | |||||
| 949 | //=== ========== ========== === | |||||
| 950 | //Wil cox, Colin is the fi rst contac t | |||||
| 951 | //=== ========== ========== === | |||||
| 952 | //Wil cox, Colin is the si ngle conta ct | |||||
| 953 | //=== ========== ========== ========== ===== | |||||
| 954 | Syste m.Console. WriteLine( ); | |||||
| 955 | ||||||
| 956 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 957 | // LNQ Retrievi ng formatt ed values 1 | |||||
| 958 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 959 | ||||||
| 960 | Syste m.Console. WriteLine( ); | |||||
| 961 | Syste m.Console. WriteLine( "Retrievin g formatte d values 1 "); | |||||
| 962 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 963 | //<sn ippetLINQE xamples32> | |||||
| 964 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 965 | { | |||||
| 966 | var list_retri eve1 = fro m c in svc Context.Co ntactSet | |||||
| 967 | whe re c.Conta ctId == _c ontactId1 | |||||
| 968 | sel ect new { StatusReas on = c.For mattedValu es["status code"] }; | |||||
| 969 | fore ach (var c in list_r etrieve1) | |||||
| 970 | { | |||||
| 971 | Sys tem.Consol e.WriteLin e("Status: " + c.Sta tusReason) ; | |||||
| 972 | } | |||||
| 973 | } | |||||
| 974 | //</s nippetLINQ Examples32 > | |||||
| 975 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 976 | // OU TPUT: | |||||
| 977 | //Ret rieving fo rmatted va lues 1 | |||||
| 978 | //=== ========== ========== ========== ===== | |||||
| 979 | //Sta tus: Activ e | |||||
| 980 | //=== ========== ========== ========== ==== | |||||
| 981 | Syste m.Console. WriteLine( ); | |||||
| 982 | ||||||
| 983 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 984 | // LNQ Using th e Skip and Take oper ators (non -paging) | |||||
| 985 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 986 | ||||||
| 987 | Syste m.Console. WriteLine( ); | |||||
| 988 | Syste m.Console. WriteLine( "List of C ontact Inf o using th e Skip and Take oper ators"); | |||||
| 989 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 990 | //<sn ippetLINQE xamples33> | |||||
| 991 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 992 | { | |||||
| 993 | ||||||
| 994 | var query_skip = (from c in svcCon text.Conta ctSet | |||||
| 995 | where c.LastName != "Parke r" | |||||
| 996 | orderb y c.FirstN ame | |||||
| 997 | select new | |||||
| 998 | { | |||||
| 999 | l ast = c.La stName, | |||||
| 1000 | f irst = c.F irstName | |||||
| 1001 | }) .Skip(2).T ake(2); | |||||
| 1002 | fore ach (var c in query_ skip) | |||||
| 1003 | { | |||||
| 1004 | Sys tem.Consol e.WriteLin e(c.first + " " + c. last); | |||||
| 1005 | } | |||||
| 1006 | } | |||||
| 1007 | //</s nippetLINQ Examples33 > | |||||
| 1008 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1009 | // OU TPUT: | |||||
| 1010 | //Lis t of Conta ct Info us ing the Sk ip and Tak e operator s | |||||
| 1011 | //=== ========== ========== ========== ===== | |||||
| 1012 | //Col in Wilcox | |||||
| 1013 | //=== ========== ========== ========== ==== | |||||
| 1014 | Syste m.Console. WriteLine( ); | |||||
| 1015 | ||||||
| 1016 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1017 | // LNQ Using th e FirstOrD efault and SingleOrD efault ope rators | |||||
| 1018 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1019 | ||||||
| 1020 | Syste m.Console. WriteLine( ); | |||||
| 1021 | Syste m.Console. WriteLine( "========= ========== ======="); | |||||
| 1022 | //<sn ippetLINQE xamples35> | |||||
| 1023 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1024 | { | |||||
| 1025 | ||||||
| 1026 | Cont act firsto rcontact = svcContex t.ContactS et.FirstOr Default(); | |||||
| 1027 | ||||||
| 1028 | Cont act single orcontact = svcConte xt.Contact Set | |||||
| 1029 | .Si ngleOrDefa ult(c => c .ContactId == _conta ctId1); | |||||
| 1030 | ||||||
| 1031 | ||||||
| 1032 | Syst em.Console .WriteLine (firstorco ntact.Full Name + | |||||
| 1033 | " i s the firs t contact" ); | |||||
| 1034 | Syst em.Console .WriteLine ("======== ========== ========") ; | |||||
| 1035 | Syst em.Console .WriteLine (singleorc ontact.Ful lName + | |||||
| 1036 | " i s the sing le contact "); | |||||
| 1037 | } | |||||
| 1038 | //</s nippetLINQ Examples35 > | |||||
| 1039 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1040 | // OU TPUT: | |||||
| 1041 | //=== ========== ========== === | |||||
| 1042 | //Col in Wilcox is the fir st contact | |||||
| 1043 | //=== ========== ========== === | |||||
| 1044 | //Col in Wilcox is the sin gle contac t | |||||
| 1045 | //=== ========== ========== ========== ===== | |||||
| 1046 | Syste m.Console. WriteLine( ); | |||||
| 1047 | ||||||
| 1048 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1049 | // LNQ Using a self join with a con dition on the linked entity | |||||
| 1050 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1051 | ||||||
| 1052 | Syste m.Console. WriteLine( ); | |||||
| 1053 | Syste m.Console. WriteLine( "List of C ontact Inf o using a self join with a con dition on the linked entity"); | |||||
| 1054 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1055 | //<sn ippetLINQE xamples36> | |||||
| 1056 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1057 | { | |||||
| 1058 | var query_join cond = fro m a1 in sv cContext.A ccountSet | |||||
| 1059 | joi n a2 in sv cContext.A ccountSet | |||||
| 1060 | on a1.ParentA ccountId.I d equals a 2.AccountI d | |||||
| 1061 | whe re a2.Acco untId == _ accountId1 | |||||
| 1062 | sel ect new { Account = a1, Parent = a2 }; | |||||
| 1063 | fore ach (var a in query_ joincond) | |||||
| 1064 | { | |||||
| 1065 | Sys tem.Consol e.WriteLin e(a.Accoun t.Name + " " + a.Par ent.Name); | |||||
| 1066 | } | |||||
| 1067 | } | |||||
| 1068 | //</s nippetLINQ Examples36 > | |||||
| 1069 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1070 | // OU TPUT: | |||||
| 1071 | //Lis t of Conta ct Info us ing a self join with a conditi on on the linked ent ity | |||||
| 1072 | //=== ========== ========== ========== ===== | |||||
| 1073 | //Con toso Ltd C oho Winery | |||||
| 1074 | //=== ========== ========== ========== ==== | |||||
| 1075 | Syste m.Console. WriteLine( ); | |||||
| 1076 | ||||||
| 1077 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1078 | // LNQ Using a transforma tion in th e where cl ause | |||||
| 1079 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1080 | ||||||
| 1081 | Syste m.Console. WriteLine( ); | |||||
| 1082 | Syste m.Console. WriteLine( "List of C ontact Inf o using a transforma tion in th e where cl ause"); | |||||
| 1083 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1084 | //<sn ippetLINQE xamples37> | |||||
| 1085 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1086 | { | |||||
| 1087 | var query_wher etrans = f rom c in s vcContext. ContactSet | |||||
| 1088 | w here c.Con tactId == _contactId 1 && | |||||
| 1089 | c .Anniversa ry > DateT ime.Parse( "1/1/2010" ) | |||||
| 1090 | s elect new | |||||
| 1091 | { | |||||
| 1092 | c.FirstNam e, | |||||
| 1093 | c.LastName | |||||
| 1094 | } ; | |||||
| 1095 | fore ach (var c in query_ wheretrans ) | |||||
| 1096 | { | |||||
| 1097 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + c.LastNa me); | |||||
| 1098 | } | |||||
| 1099 | } | |||||
| 1100 | //</s nippetLINQ Examples37 > | |||||
| 1101 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1102 | // OU TPUT: | |||||
| 1103 | // Li st of Cont act Info u sing a tra nsformatio n in the w here claus e | |||||
| 1104 | //=== ========== ========== ========== ===== | |||||
| 1105 | //Col in Wilcox | |||||
| 1106 | //=== ========== ========== ========== ==== | |||||
| 1107 | Syste m.Console. WriteLine( ); | |||||
| 1108 | ||||||
| 1109 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1110 | // LNQ Using a lookup val ue to orde r by | |||||
| 1111 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1112 | ||||||
| 1113 | Syste m.Console. WriteLine( ); | |||||
| 1114 | Syste m.Console. WriteLine( "List of A ccount Inf o using a lookup val ue to orde r by"); | |||||
| 1115 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1116 | //<sn ippetLINQE xamples38> | |||||
| 1117 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1118 | { | |||||
| 1119 | var query_orde rbylookup = from a i n svcConte xt.Account Set | |||||
| 1120 | where a. Address1_N ame == "Co ntoso Phar maceutical s" | |||||
| 1121 | orderby a.PrimaryC ontactId | |||||
| 1122 | select n ew | |||||
| 1123 | { | |||||
| 1124 | a.Name, | |||||
| 1125 | a.Addre ss1_City | |||||
| 1126 | }; | |||||
| 1127 | fore ach (var a in query_ orderbyloo kup) | |||||
| 1128 | { | |||||
| 1129 | Sys tem.Consol e.WriteLin e(a.Name + " " + a.A ddress1_Ci ty); | |||||
| 1130 | } | |||||
| 1131 | } | |||||
| 1132 | //</s nippetLINQ Examples38 > | |||||
| 1133 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1134 | // OU TPUT: | |||||
| 1135 | // Li st of Acco unt Info u sing a loo kup value to order b y | |||||
| 1136 | //=== ========== ========== ========== ===== | |||||
| 1137 | //Con toso Ltd R edmond | |||||
| 1138 | //=== ========== ========== ========== ==== | |||||
| 1139 | Syste m.Console. WriteLine( ); | |||||
| 1140 | ||||||
| 1141 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1142 | // LNQ Using a a picklist to order by | |||||
| 1143 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1144 | ||||||
| 1145 | Syste m.Console. WriteLine( ); | |||||
| 1146 | Syste m.Console. WriteLine( "List of C ontact Inf o using a picklist t o order by "); | |||||
| 1147 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1148 | //<sn ippetLINQE xamples39> | |||||
| 1149 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1150 | { | |||||
| 1151 | var query_orde rbypicklis t = from c in svcCon text.Conta ctSet | |||||
| 1152 | where c.LastName != "Parke r" && | |||||
| 1153 | c.Acco untRoleCod e != null | |||||
| 1154 | orderb y c.Accoun tRoleCode, c.FirstNa me | |||||
| 1155 | select new | |||||
| 1156 | { | |||||
| 1157 | Accou ntRole = c .Formatted Values["ac countrolec ode"], | |||||
| 1158 | c.Fir stName, | |||||
| 1159 | c.Las tName | |||||
| 1160 | }; | |||||
| 1161 | fore ach (var c in query_ orderbypic klist) | |||||
| 1162 | { | |||||
| 1163 | Sys tem.Consol e.WriteLin e(c.Accoun tRole + " " + | |||||
| 1164 | c. FirstName + " " + c. LastName); | |||||
| 1165 | } | |||||
| 1166 | } | |||||
| 1167 | //</s nippetLINQ Examples39 > | |||||
| 1168 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1169 | // OU TPUT: | |||||
| 1170 | //Lis t of Conta ct Info us ing a pick list to or der by | |||||
| 1171 | //=== ========== ========== ========== ===== | |||||
| 1172 | //Dec ision Make r Colin Wi lcox | |||||
| 1173 | //Emp loyee Ben Smith | |||||
| 1174 | //Emp loyee Bria n Smith | |||||
| 1175 | //=== ========== ========== ========== ==== | |||||
| 1176 | Syste m.Console. WriteLine( ); | |||||
| 1177 | ||||||
| 1178 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1179 | // LNQ Using a paging sor t 1 | |||||
| 1180 | // ( Multi-colu mn sort wi th extra c ondition) | |||||
| 1181 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1182 | ||||||
| 1183 | Syste m.Console. WriteLine( ); | |||||
| 1184 | Syste m.Console. WriteLine( "List of C ontact Inf o using a paging sor t 1"); | |||||
| 1185 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1186 | //<sn ippetLINQE xamples41> | |||||
| 1187 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1188 | { | |||||
| 1189 | var query_pagi ngsort1 = (from c in svcContex t.ContactS et | |||||
| 1190 | where c.L astName != "Parker" | |||||
| 1191 | orderby c .LastName ascending, | |||||
| 1192 | c.FirstNa me descend ing | |||||
| 1193 | select ne w { c.Firs tName, c.L astName }) | |||||
| 1194 | .Skip(2). Take(2); | |||||
| 1195 | fore ach (var c in query_ pagingsort 1) | |||||
| 1196 | { | |||||
| 1197 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + c.LastNa me); | |||||
| 1198 | } | |||||
| 1199 | } | |||||
| 1200 | //</s nippetLINQ Examples41 > | |||||
| 1201 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1202 | // OU TPUT: | |||||
| 1203 | // List of Contact In fo using a paging so rt 1 | |||||
| 1204 | //=== ========== ========== ========== ===== | |||||
| 1205 | //Col in Wilcox | |||||
| 1206 | //=== ========== ========== ========== ==== | |||||
| 1207 | Syste m.Console. WriteLine( ); | |||||
| 1208 | ||||||
| 1209 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1210 | // LNQ Using a paging sor t 2 | |||||
| 1211 | // (Page and sort wher e the colu mn being s orted is d ifferent f rom the co lumn being retrieved | |||||
| 1212 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1213 | ||||||
| 1214 | Syste m.Console. WriteLine( ); | |||||
| 1215 | Syste m.Console. WriteLine( "List of C ontact Inf o using a paging sor t 2"); | |||||
| 1216 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1217 | //<sn ippetLINQE xamples42> | |||||
| 1218 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1219 | { | |||||
| 1220 | var query_pagi ngsort2 = (from c in svcContex t.ContactS et | |||||
| 1221 | where c.L astName != "Parker" | |||||
| 1222 | orderby c .FirstName descendin g | |||||
| 1223 | select ne w { c.Firs tName }).S kip(2).Tak e(2); | |||||
| 1224 | fore ach (var c in query_ pagingsort 2) | |||||
| 1225 | { | |||||
| 1226 | Sys tem.Consol e.WriteLin e(c.FirstN ame); | |||||
| 1227 | } | |||||
| 1228 | } | |||||
| 1229 | //</s nippetLINQ Examples42 > | |||||
| 1230 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1231 | // OU TPUT: | |||||
| 1232 | // List of Contact In fo using a paging so rt 2 | |||||
| 1233 | //=== ========== ========== ========== ===== | |||||
| 1234 | //Ben | |||||
| 1235 | //=== ========== ========== ========== ==== | |||||
| 1236 | Syste m.Console. WriteLine( ); | |||||
| 1237 | ||||||
| 1238 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1239 | // LNQ Using a paging sor t 3 | |||||
| 1240 | // (Cr eates only the first page) | |||||
| 1241 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1242 | ||||||
| 1243 | Syste m.Console. WriteLine( ); | |||||
| 1244 | Syste m.Console. WriteLine( "List of C ontact Inf o using a paging sor t 3"); | |||||
| 1245 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1246 | //<sn ippetLINQE xamples43> | |||||
| 1247 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1248 | { | |||||
| 1249 | var query_pagi ngsort3 = (from c in svcContex t.ContactS et | |||||
| 1250 | where c.L astName.St artsWith(" W") | |||||
| 1251 | orderby c .MiddleNam e ascendin g, | |||||
| 1252 | c.FirstNa me descend ing | |||||
| 1253 | select ne w | |||||
| 1254 | { | |||||
| 1255 | c.FirstN ame, | |||||
| 1256 | c.Middle Name, | |||||
| 1257 | c.LastNa me | |||||
| 1258 | }).Take(1 0); | |||||
| 1259 | fore ach (var c in query_ pagingsort 3) | |||||
| 1260 | { | |||||
| 1261 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + | |||||
| 1262 | c. MiddleName + " " + c .LastName) ; | |||||
| 1263 | } | |||||
| 1264 | } | |||||
| 1265 | //</s nippetLINQ Examples43 > | |||||
| 1266 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1267 | ||||||
| 1268 | // OU TPUT: | |||||
| 1269 | //Lis t of Conta ct Info us ing a pagi ng sort 3 | |||||
| 1270 | //=== ========== ========== ========== ===== | |||||
| 1271 | //Col in Wilcox | |||||
| 1272 | //=== ========== ========== ========== ==== | |||||
| 1273 | Syste m.Console. WriteLine( ); | |||||
| 1274 | ||||||
| 1275 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1276 | // LNQ Retrievi ng related entity co lumns (for 1 to N re lationship s) | |||||
| 1277 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1278 | ||||||
| 1279 | Syste m.Console. WriteLine( ); | |||||
| 1280 | Syste m.Console. WriteLine( "List of C ontact and Account I nfo by ret rieving re lated enti ty columns "); | |||||
| 1281 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1282 | //<sn ippetLINQE xamples44> | |||||
| 1283 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1284 | { | |||||
| 1285 | var query_retr ieve1 = fr om c in sv cContext.C ontactSet | |||||
| 1286 | jo in a in sv cContext.A ccountSet | |||||
| 1287 | on c.Contact Id equals a.PrimaryC ontactId.I d | |||||
| 1288 | wh ere c.Cont actId != _ contactId1 | |||||
| 1289 | se lect new { Contact = c, Accoun t = a }; | |||||
| 1290 | fore ach (var c in query_ retrieve1) | |||||
| 1291 | { | |||||
| 1292 | Sys tem.Consol e.WriteLin e("Acct: " + c.Accou nt.Name + | |||||
| 1293 | "\ t\t" + "Co ntact: " + c.Contact .FullName) ; | |||||
| 1294 | } | |||||
| 1295 | } | |||||
| 1296 | //</s nippetLINQ Examples44 > | |||||
| 1297 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1298 | // OU TPUT: | |||||
| 1299 | // List of Contact an d Account Info by re trieving r elated ent ity column s | |||||
| 1300 | //=== ========== ========== ========== ===== | |||||
| 1301 | //Acc t: Contoso Ltd C ontact: Br ian Smith | |||||
| 1302 | //=== ========== ========== ========== ==== | |||||
| 1303 | Syste m.Console. WriteLine( ); | |||||
| 1304 | ||||||
| 1305 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1306 | // LNQ Using .V alue to re trieve the value of an attribu te | |||||
| 1307 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1308 | ||||||
| 1309 | Syste m.Console. WriteLine( ); | |||||
| 1310 | Syste m.Console. WriteLine( "List of C ontact Inf o using .V alue to re trieve the value of an attribu te"); | |||||
| 1311 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1312 | //<sn ippetLINQE xamples45> | |||||
| 1313 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1314 | { | |||||
| 1315 | ||||||
| 1316 | var query_valu e = from c in svcCon text.Conta ctSet | |||||
| 1317 | where c.ContactI d != _cont actId2 | |||||
| 1318 | select new | |||||
| 1319 | { | |||||
| 1320 | Conta ctId = c.C ontactId ! = null ? | |||||
| 1321 | c.Co ntactId.Va lue : Guid .Empty, | |||||
| 1322 | Numbe rOfChildre n = c.Numb erOfChildr en != null ? | |||||
| 1323 | c.Nu mberOfChil dren.Value : default (int), | |||||
| 1324 | Credi tOnHold = c.CreditOn Hold != nu ll ? | |||||
| 1325 | c.Cr editOnHold .Value : d efault(boo l), | |||||
| 1326 | Anniv ersary = c .Anniversa ry != null ? | |||||
| 1327 | c.An niversary. Value : de fault(Date Time) | |||||
| 1328 | }; | |||||
| 1329 | ||||||
| 1330 | fore ach (var c in query_ value) | |||||
| 1331 | { | |||||
| 1332 | Sys tem.Consol e.WriteLin e(c.Contac tId + " " + c.Number OfChildren + | |||||
| 1333 | " " + c.Cred itOnHold + " " + c.A nniversary ); | |||||
| 1334 | } | |||||
| 1335 | } | |||||
| 1336 | //</s nippetLINQ Examples45 > | |||||
| 1337 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1338 | // OU TPUT: | |||||
| 1339 | //Lis t of Conta ct Info us ing .Value to retrie ve the val ue of an a ttribute | |||||
| 1340 | //=== ========== ========== ========== ===== | |||||
| 1341 | //8f8 228af-04a1 -e011-b1b7 -00155dba3 818 1 Fals e 3/5/2010 8:00:00 A M | |||||
| 1342 | //938 228af-04a1 -e011-b1b7 -00155dba3 818 2 Fals e 10/5/201 0 7:00:00 AM | |||||
| 1343 | //958 228af-04a1 -e011-b1b7 -00155dba3 818 2 True 7/5/2010 7:00:00 AM | |||||
| 1344 | //=== ========== ========== ========== ==== | |||||
| 1345 | Syste m.Console. WriteLine( ); | |||||
| 1346 | ||||||
| 1347 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1348 | // LNQ Multiple projectio ns, new da ta type ca sting to d ifferent t ypes | |||||
| 1349 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1350 | ||||||
| 1351 | Syste m.Console. WriteLine( ); | |||||
| 1352 | Syste m.Console. WriteLine( "List of C ontact Inf o using mu ltiple pro jections") ; | |||||
| 1353 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1354 | //<sn ippetLINQE xamples46> | |||||
| 1355 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1356 | { | |||||
| 1357 | var query_proj ections = from c in svcContext .ContactSe t | |||||
| 1358 | where c.Co ntactId == _contactI d1 | |||||
| 1359 | && c.Numbe rOfChildre n != null && | |||||
| 1360 | c.Annivers ary.Value != null | |||||
| 1361 | select new | |||||
| 1362 | { | |||||
| 1363 | Contact = new Conta ct { | |||||
| 1364 | LastName = c.LastN ame, | |||||
| 1365 | NumberOf Children = c.NumberO fChildren | |||||
| 1366 | }, | |||||
| 1367 | NumberOfC hildren = (double)c. NumberOfCh ildren, | |||||
| 1368 | Anniversa ry = c.Ann iversary.V alue.AddYe ars(1), | |||||
| 1369 | }; | |||||
| 1370 | fore ach (var c in query_ projection s) | |||||
| 1371 | { | |||||
| 1372 | Sys tem.Consol e.WriteLin e(c.Contac t.LastName + " " + | |||||
| 1373 | c. NumberOfCh ildren + " " + c.Ann iversary); | |||||
| 1374 | } | |||||
| 1375 | } | |||||
| 1376 | //</s nippetLINQ Examples46 > | |||||
| 1377 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1378 | // OU TPUT: | |||||
| 1379 | //Lis t of Conta ct Info us ing multip le project ions | |||||
| 1380 | //=== ========== ========== ========== ===== | |||||
| 1381 | //Wil cox 1 3/5/ 2011 8:00: 00 AM | |||||
| 1382 | //=== ========== ========== ========== ==== | |||||
| 1383 | Syste m.Console. WriteLine( ); | |||||
| 1384 | ||||||
| 1385 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1386 | // LNQ Using th e GetAttri buteValue method | |||||
| 1387 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1388 | ||||||
| 1389 | Syste m.Console. WriteLine( ); | |||||
| 1390 | Syste m.Console. WriteLine( "List of C ontact Inf o using th e GetAttri buteValue method"); | |||||
| 1391 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1392 | //<sn ippetLINQE xamples47> | |||||
| 1393 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1394 | { | |||||
| 1395 | var query_geta ttrib = fr om c in sv cContext.C ontactSet | |||||
| 1396 | wh ere c.GetA ttributeVa lue<Guid>( "contactid ") != _con tactId1 | |||||
| 1397 | se lect new | |||||
| 1398 | { | |||||
| 1399 | C ontactId = c.GetAttr ibuteValue <Guid?>("c ontactid") , | |||||
| 1400 | N umberOfChi ldren = c. GetAttribu teValue<in t?>("numbe rofchildre n"), | |||||
| 1401 | C reditOnHol d = c.GetA ttributeVa lue<bool?> ("crediton hold"), | |||||
| 1402 | A nniversary = c.GetAt tributeVal ue<DateTim e?>("anniv ersary"), | |||||
| 1403 | }; | |||||
| 1404 | ||||||
| 1405 | fore ach (var c in query_ getattrib) | |||||
| 1406 | { | |||||
| 1407 | Sys tem.Consol e.WriteLin e(c.Contac tId + " " + c.Number OfChildren + | |||||
| 1408 | " " + c.Cred itOnHold + " " + c.A nniversary ); | |||||
| 1409 | } | |||||
| 1410 | } | |||||
| 1411 | //</s nippetLINQ Examples47 > | |||||
| 1412 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1413 | // OU TPUT: | |||||
| 1414 | //Lis t of Conta ct Info us ing the Ge tAttribute Value meth od | |||||
| 1415 | //=== ========== ========== ========== ===== | |||||
| 1416 | //918 228af-04a1 -e011-b1b7 -00155dba3 818 2 Fals e 4/5/2010 7:00:00 A M | |||||
| 1417 | //938 228af-04a1 -e011-b1b7 -00155dba3 818 2 Fals e 10/5/201 0 7:00:00 AM | |||||
| 1418 | //958 228af-04a1 -e011-b1b7 -00155dba3 818 2 True 7/5/2010 7:00:00 AM | |||||
| 1419 | //=== ========== ========== ========== ==== | |||||
| 1420 | Syste m.Console. WriteLine( ); | |||||
| 1421 | ||||||
| 1422 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1423 | // LNQ Using ma th operati ons | |||||
| 1424 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1425 | ||||||
| 1426 | Syste m.Console. WriteLine( ); | |||||
| 1427 | Syste m.Console. WriteLine( "List of C ontact Inf o using ma th operati ons"); | |||||
| 1428 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1429 | //<sn ippetLINQE xamples48> | |||||
| 1430 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1431 | { | |||||
| 1432 | var query_math = from c in svcCont ext.Contac tSet | |||||
| 1433 | where c .ContactId != _conta ctId2 | |||||
| 1434 | && c.Ad dress1_Lat itude != n ull && | |||||
| 1435 | c.Addre ss1_Longit ude != nul l | |||||
| 1436 | select new | |||||
| 1437 | { | |||||
| 1438 | Round = Math.Rou nd(c.Addre ss1_Latitu de.Value), | |||||
| 1439 | Floor = Math.Flo or(c.Addre ss1_Latitu de.Value), | |||||
| 1440 | Ceilin g = Math.C eiling(c.A ddress1_La titude.Val ue), | |||||
| 1441 | Abs = Math.Abs(c .Address1_ Latitude.V alue), | |||||
| 1442 | }; | |||||
| 1443 | fore ach (var c in query_ math) | |||||
| 1444 | { | |||||
| 1445 | Sys tem.Consol e.WriteLin e(c.Round + " " + c. Floor + | |||||
| 1446 | " " + c.Ceil ing + " " + c.Abs); | |||||
| 1447 | } | |||||
| 1448 | } | |||||
| 1449 | //</s nippetLINQ Examples48 > | |||||
| 1450 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1451 | // OU TPUT: | |||||
| 1452 | // List of Contact In fo using m ath operat ions | |||||
| 1453 | //=== ========== ========== ========== ===== | |||||
| 1454 | //48 47 48 47.6 7417 | |||||
| 1455 | //48 47 48 47.6 1056 | |||||
| 1456 | //48 47 48 47.6 1056 | |||||
| 1457 | //=== ========== ========== ========== ==== | |||||
| 1458 | Syste m.Console. WriteLine( ); | |||||
| 1459 | ||||||
| 1460 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1461 | // LNQ Using mu ltiple sel ect and wh ere clause s | |||||
| 1462 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1463 | ||||||
| 1464 | Syste m.Console. WriteLine( ); | |||||
| 1465 | Syste m.Console. WriteLine( "List of I ncidents u sing multi ple select and where clauses") ; | |||||
| 1466 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1467 | //<sn ippetLINQE xamples49> | |||||
| 1468 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1469 | { | |||||
| 1470 | var query_mult iselect = svcContext .IncidentS et | |||||
| 1471 | . Where(i => i.Inciden tId != _in cidentId1) | |||||
| 1472 | . Select(i = > i.incide nt_custome r_accounts ) | |||||
| 1473 | . Where(a => a.Account Id != _acc ountId2) | |||||
| 1474 | . Select(a = > a.accoun t_primary_ contact) | |||||
| 1475 | . OrderBy(c => c.First Name) | |||||
| 1476 | . Select(c = > c.Contac tId); | |||||
| 1477 | fore ach (var c in query_ multiselec t) | |||||
| 1478 | { | |||||
| 1479 | Sys tem.Consol e.WriteLin e(c.GetVal ueOrDefaul t()); | |||||
| 1480 | } | |||||
| 1481 | } | |||||
| 1482 | //</s nippetLINQ Examples49 > | |||||
| 1483 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1484 | // OU TPUT: | |||||
| 1485 | //Lis t of Incid ents using multiple select and where cla uses | |||||
| 1486 | //=== ========== ========== ========== ===== | |||||
| 1487 | //918 228af-04a1 -e011-b1b7 -00155dba3 818 | |||||
| 1488 | //=== ========== ========== ========== ==== | |||||
| 1489 | Syste m.Console. WriteLine( ); | |||||
| 1490 | ||||||
| 1491 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1492 | // LNQ Using Se lectMany | |||||
| 1493 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1494 | ||||||
| 1495 | Syste m.Console. WriteLine( ); | |||||
| 1496 | Syste m.Console. WriteLine( "List of A ccount Inf o using Se lectMany") ; | |||||
| 1497 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1498 | //<sn ippetLINQE xamples50> | |||||
| 1499 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1500 | { | |||||
| 1501 | var query_sele ctmany = s vcContext. ContactSet | |||||
| 1502 | . Where(c => c.Contact Id != _con tactId2) | |||||
| 1503 | . SelectMany (c => c.ac count_prim ary_contac t) | |||||
| 1504 | . OrderBy(a => a.Name) ; | |||||
| 1505 | fore ach (var c in query_ selectmany ) | |||||
| 1506 | { | |||||
| 1507 | Sys tem.Consol e.WriteLin e(c.Accoun tId + " " + c.Name); | |||||
| 1508 | } | |||||
| 1509 | } | |||||
| 1510 | //</s nippetLINQ Examples50 > | |||||
| 1511 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1512 | // OU TPUT: | |||||
| 1513 | //Lis t of Accou nt Info us ing Select Many | |||||
| 1514 | //=== ========== ========== ========== ===== | |||||
| 1515 | //9b8 228af-04a1 -e011-b1b7 -00155dba3 818 Contos o Ltd | |||||
| 1516 | //=== ========== ========== ========== ==== | |||||
| 1517 | Syste m.Console. WriteLine( ); | |||||
| 1518 | ||||||
| 1519 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1520 | // LNQ Using St ring opera tions | |||||
| 1521 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1522 | ||||||
| 1523 | Syste m.Console. WriteLine( ); | |||||
| 1524 | Syste m.Console. WriteLine( "List of C ontact Inf o using St ring opera tions"); | |||||
| 1525 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1526 | //<sn ippetLINQE xamples51> | |||||
| 1527 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1528 | { | |||||
| 1529 | var query_stri ng = from c in svcCo ntext.Cont actSet | |||||
| 1530 | where c.Contact Id == _con tactId2 | |||||
| 1531 | selec t new | |||||
| 1532 | { | |||||
| 1533 | Inde xOf = c.Fi rstName.In dexOf("con tact"), | |||||
| 1534 | Inse rt = c.Fir stName.Ins ert(1, "In sert"), | |||||
| 1535 | Remo ve = c.Fir stName.Rem ove(1, 1), | |||||
| 1536 | Subs tring = c. FirstName. Substring( 1, 1), | |||||
| 1537 | ToUp per = c.Fi rstName.To Upper(), | |||||
| 1538 | ToLo wer = c.Fi rstName.To Lower(), | |||||
| 1539 | Trim Start = c. FirstName. TrimStart( ), | |||||
| 1540 | Trim End = c.Fi rstName.Tr imEnd(), | |||||
| 1541 | }; | |||||
| 1542 | ||||||
| 1543 | fore ach (var c in query_ string) | |||||
| 1544 | { | |||||
| 1545 | Sys tem.Consol e.WriteLin e(c.IndexO f + "\n" + c.Insert + "\n" + | |||||
| 1546 | c. Remove + " \n" + c.Su bstring + "\n" | |||||
| 1547 | + c.ToUp per + "\n" + c.ToLow er + | |||||
| 1548 | "\n" + c .TrimStart + " " + c .TrimEnd); | |||||
| 1549 | } | |||||
| 1550 | } | |||||
| 1551 | //</s nippetLINQ Examples51 > | |||||
| 1552 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1553 | // OU TPUT: | |||||
| 1554 | //Lis t of Conta ct Info us ing String operation s | |||||
| 1555 | //=== ========== ========== ========== ===== | |||||
| 1556 | //-1 | |||||
| 1557 | //BIn sertrian | |||||
| 1558 | //Bia n | |||||
| 1559 | //r | |||||
| 1560 | //BRI AN | |||||
| 1561 | //bri an | |||||
| 1562 | //Bri an Brian | |||||
| 1563 | //=== ========== ========== ========== ==== | |||||
| 1564 | Syste m.Console. WriteLine( ); | |||||
| 1565 | ||||||
| 1566 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1567 | // LNQ Using Tw o Where Cl auses | |||||
| 1568 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1569 | ||||||
| 1570 | Syste m.Console. WriteLine( ); | |||||
| 1571 | Syste m.Console. WriteLine( "List of A ccount and Contact I nfo using two where clauses"); | |||||
| 1572 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1573 | //<sn ippetLINQE xamples52> | |||||
| 1574 | using (ServiceC ontext svc Context = new Servic eContext(_ servicePro xy)) | |||||
| 1575 | { | |||||
| 1576 | var query_twow here = fro m a in svc Context.Ac countSet | |||||
| 1577 | joi n c in svc Context.Co ntactSet | |||||
| 1578 | on a.PrimaryC ontactId.I d equals c .ContactId | |||||
| 1579 | whe re c.LastN ame == "Sm ith" && c. CreditOnHo ld != null | |||||
| 1580 | whe re a.Name == "Contos o Ltd" | |||||
| 1581 | ord erby a.Nam e | |||||
| 1582 | sel ect a; | |||||
| 1583 | fore ach (var c in query_ twowhere) | |||||
| 1584 | { | |||||
| 1585 | Sys tem.Consol e.WriteLin e(c.Accoun tId + " " + c.Name); | |||||
| 1586 | } | |||||
| 1587 | } | |||||
| 1588 | //</s nippetLINQ Examples52 > | |||||
| 1589 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1590 | // OU TPUT: | |||||
| 1591 | //Lis t of Accou nt and Con tact Info using two where clau ses | |||||
| 1592 | //=== ========== ========== ========== ===== | |||||
| 1593 | //9b8 228af-04a1 -e011-b1b7 -00155dba3 818 Contos o Ltd | |||||
| 1594 | //=== ========== ========== ========== ==== | |||||
| 1595 | Syste m.Console. WriteLine( ); | |||||
| 1596 | ||||||
| 1597 | ||||||
| 1598 | #endr egion Earl yBoundExam ples | |||||
| 1599 | ||||||
| 1600 | #regi on LateBou ndExamples | |||||
| 1601 | ||||||
| 1602 | ||||||
| 1603 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1604 | // LNQ Us ing late-b ound entit ies, simpl e inner jo in 2 | |||||
| 1605 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1606 | ||||||
| 1607 | Syste m.Console. WriteLine( ); | |||||
| 1608 | Syste m.Console. WriteLine( "List of C ontact and Account I nfo using late-bound entities with join 2"); | |||||
| 1609 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 1610 | //<sn ippetLINQE xamples6> | |||||
| 1611 | using (Organiza tionServic eContext o rgSvcConte xt = new O rganizatio nServiceCo ntext(_ser viceProxy) ) | |||||
| 1612 | { | |||||
| 1613 | var query_join 2 = from c in orgSvc Context.Cr eateQuery( "contact") | |||||
| 1614 | join a in orgSvc Context.Cr eateQuery( "account") | |||||
| 1615 | on c[" contactid" ] equals a ["primaryc ontactid"] | |||||
| 1616 | select new | |||||
| 1617 | { | |||||
| 1618 | conta ct_name = c["fullnam e"], | |||||
| 1619 | accou nt_name = a["name"] | |||||
| 1620 | }; | |||||
| 1621 | fore ach (var c in query_ join2) | |||||
| 1622 | { | |||||
| 1623 | Sys tem.Consol e.WriteLin e(c.contac t_name + " " + c.ac count_name ); | |||||
| 1624 | } | |||||
| 1625 | } | |||||
| 1626 | //</s nippetLINQ Examples6> | |||||
| 1627 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 1628 | // OU TPUT: | |||||
| 1629 | //Lis t of Conta ct and Acc ount Info using late -bound ent ities with join 2 | |||||
| 1630 | //=== ========== ========== ========== = | |||||
| 1631 | //Bri an Smith Contoso Lt d | |||||
| 1632 | //=== ========== ========== ========== = | |||||
| 1633 | Syste m.Console. WriteLine( ); | |||||
| 1634 | ||||||
| 1635 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1636 | // LNQ Al ternative syntax sim ple inner join 3 | |||||
| 1637 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1638 | ||||||
| 1639 | Syste m.Console. WriteLine( ); | |||||
| 1640 | Syste m.Console. WriteLine( "List of C ontact and Account I nfo with a lternative syntax jo in 3"); | |||||
| 1641 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 1642 | ||||||
| 1643 | //<sn ippetLINQE xamples7> | |||||
| 1644 | using (Organiza tionServic eContext o rgSvcConte xt = new O rganizatio nServiceCo ntext(_ser viceProxy) ) | |||||
| 1645 | { | |||||
| 1646 | var query_join 3 = from c in orgSvc Context.Cr eateQuery( "contact") | |||||
| 1647 | join a in orgSvc Context.Cr eateQuery( "account") | |||||
| 1648 | on c[" contactid" ] equals ( Guid)((Ent ityReferen ce)a["prim arycontact id"]).Id | |||||
| 1649 | select new | |||||
| 1650 | { | |||||
| 1651 | conta ct_name = c["fullnam e"], | |||||
| 1652 | accou nt_name = a["name"] | |||||
| 1653 | }; | |||||
| 1654 | ||||||
| 1655 | fore ach (var c in query_ join3) | |||||
| 1656 | { | |||||
| 1657 | Sys tem.Consol e.WriteLin e(c.contac t_name + " " + c.ac count_name ); | |||||
| 1658 | } | |||||
| 1659 | } | |||||
| 1660 | //</s nippetLINQ Examples7> | |||||
| 1661 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 1662 | // OU TPUT: | |||||
| 1663 | //Lis t of Conta ct and Acc ount Info with alter native syn tax join 3 | |||||
| 1664 | //=== ========== ========== ========== = | |||||
| 1665 | //Bri an Smith Contoso Lt d | |||||
| 1666 | //=== ========== ========== ========== = | |||||
| 1667 | Syste m.Console. WriteLine( ); | |||||
| 1668 | ||||||
| 1669 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1670 | // LNQ Late bin ding left join 9 | |||||
| 1671 | // A left join is design ed to retu rn parents with and without ch ildren fro m two sour ces. | |||||
| 1672 | // There is a correlati on between parent an d child, b ut no chil d may actu ally exist . | |||||
| 1673 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1674 | ||||||
| 1675 | Syste m.Console. WriteLine( ); | |||||
| 1676 | Syste m.Console. WriteLine( "List of C ontact Inf o using la te binding left join 9"); | |||||
| 1677 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 1678 | //<sn ippetLINQE xamples13> | |||||
| 1679 | using (Organiza tionServic eContext o rgSvcConte xt = new O rganizatio nServiceCo ntext(_ser viceProxy) ) | |||||
| 1680 | { | |||||
| 1681 | var query_join 9 = from a in orgSvc Context.Cr eateQuery( "account") | |||||
| 1682 | join c in orgSvc Context.Cr eateQuery( "contact") | |||||
| 1683 | on a[" primarycon tactid"] e quals c["c ontactid"] into gr | |||||
| 1684 | from c _joined in gr.Defaul tIfEmpty() | |||||
| 1685 | select new | |||||
| 1686 | { | |||||
| 1687 | accou nt_name = a.Attribut es["name"] | |||||
| 1688 | }; | |||||
| 1689 | fore ach (var c in query_ join9) | |||||
| 1690 | { | |||||
| 1691 | Sys tem.Consol e.WriteLin e(c.accoun t_name); | |||||
| 1692 | } | |||||
| 1693 | } | |||||
| 1694 | //</s nippetLINQ Examples13 > | |||||
| 1695 | Syste m.Console. WriteLine( "========= ========== ========== ====="); | |||||
| 1696 | // OU TPUT: | |||||
| 1697 | //Lis t of Conta ct Info us ing late b inding lef t join 9 | |||||
| 1698 | //=== ========== ========== ========== = | |||||
| 1699 | //Coh o Winery | |||||
| 1700 | //Con toso Ltd | |||||
| 1701 | //=== ========== ========== ========== = | |||||
| 1702 | Syste m.Console. WriteLine( ); | |||||
| 1703 | ||||||
| 1704 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1705 | // LNQ Using la te binding and not e quals oper ator 3 | |||||
| 1706 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1707 | ||||||
| 1708 | Syste m.Console. WriteLine( ); | |||||
| 1709 | Syste m.Console. WriteLine( "List of C ontact Inf o using la te binding and not e quals oper ator 3"); | |||||
| 1710 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1711 | ||||||
| 1712 | //<sn ippetLINQE xamples19> | |||||
| 1713 | using (Organiza tionServic eContext o rgSvcConte xt = new O rganizatio nServiceCo ntext(_ser viceProxy) ) | |||||
| 1714 | { | |||||
| 1715 | var query_ne3 = from c i n orgSvcCo ntext.Crea teQuery("c ontact") | |||||
| 1716 | where !c ["address1 _city"].Eq uals(null) | |||||
| 1717 | select n ew | |||||
| 1718 | { | |||||
| 1719 | FirstNa me = c["fi rstname"], | |||||
| 1720 | LastNam e = c["las tname"], | |||||
| 1721 | Address 1_City = c ["address1 _city"] | |||||
| 1722 | }; | |||||
| 1723 | fore ach (var c in query_ ne3) | |||||
| 1724 | { | |||||
| 1725 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + | |||||
| 1726 | c. LastName + " " + c.A ddress1_Ci ty); | |||||
| 1727 | } | |||||
| 1728 | } | |||||
| 1729 | //</s nippetLINQ Examples19 > | |||||
| 1730 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1731 | // OU TPUT: | |||||
| 1732 | // Li st of Cont act Info u sing late binding an d not equa ls operato r 3 | |||||
| 1733 | //=== ========== ========== ========== ===== | |||||
| 1734 | //Col in Wilcox Redmond | |||||
| 1735 | //Bri an Smith B ellevue | |||||
| 1736 | //Dar ren Parker Kirkland | |||||
| 1737 | //Ben Smith Kir kland | |||||
| 1738 | //=== ========== ========== ========== ==== | |||||
| 1739 | Syste m.Console. WriteLine( ); | |||||
| 1740 | ||||||
| 1741 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1742 | // LNQ Using la te binding with Cont ains opera tor 3 | |||||
| 1743 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1744 | ||||||
| 1745 | Syste m.Console. WriteLine( ); | |||||
| 1746 | Syste m.Console. WriteLine( "List of C ontact Inf o using la te binding with Cont ains opera tor 3"); | |||||
| 1747 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1748 | //<sn ippetLINQE xamples25> | |||||
| 1749 | using (Organiza tionServic eContext o rgSvcConte xt = new O rganizatio nServiceCo ntext(_ser viceProxy) ) | |||||
| 1750 | { | |||||
| 1751 | var query_cont ains3 = fr om c in or gSvcContex t.CreateQu ery("conta ct") | |||||
| 1752 | wh ere ((stri ng)c["desc ription"]) .Contains( "Coho") | |||||
| 1753 | se lect new | |||||
| 1754 | { | |||||
| 1755 | f irstname = c.Attribu tes["first name"], | |||||
| 1756 | l astname = c.Attribut es["lastna me"] | |||||
| 1757 | }; | |||||
| 1758 | fore ach (var c in query_ contains3) | |||||
| 1759 | { | |||||
| 1760 | Sys tem.Consol e.WriteLin e(c.firstn ame + " " + c.lastna me); | |||||
| 1761 | } | |||||
| 1762 | } | |||||
| 1763 | //</s nippetLINQ Examples25 > | |||||
| 1764 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1765 | // OU TPUT: | |||||
| 1766 | //Lis t of Conta ct Info us ing late b inding wit h Contains operator 3 | |||||
| 1767 | //=== ========== ========== ========== ===== | |||||
| 1768 | //Bri an Smith | |||||
| 1769 | //Dar ren Parker | |||||
| 1770 | //Ben Smith | |||||
| 1771 | //=== ========== ========== ========== ==== | |||||
| 1772 | Syste m.Console. WriteLine( ); | |||||
| 1773 | ||||||
| 1774 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1775 | // LNQ Using th e GetAttri buteValue (late bind ing) metho d 1 | |||||
| 1776 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1777 | Syste m.Console. WriteLine( ); | |||||
| 1778 | Syste m.Console. WriteLine( "List of C ontact Inf o using la te binding and the G etAttribut eValue met hod 2"); | |||||
| 1779 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1780 | //<sn ippetLINQE xamples34> | |||||
| 1781 | using (Organiza tionServic eContext o rgSvcConte xt = new O rganizatio nServiceCo ntext(_ser viceProxy) ) | |||||
| 1782 | { | |||||
| 1783 | ||||||
| 1784 | var list_getat trib1 = (f rom c in o rgSvcConte xt.CreateQ uery("cont act") | |||||
| 1785 | w here c.Get AttributeV alue<Guid? >("contact id") != _c ontactId1 | |||||
| 1786 | s elect new { | |||||
| 1787 | FirstName = c.GetAtt ributeValu e<string>( "firstname "), | |||||
| 1788 | LastName = c.GetAttr ibuteValue <string>(" lastname") | |||||
| 1789 | } ).ToList() ; | |||||
| 1790 | fore ach (var c in list_g etattrib1) | |||||
| 1791 | { | |||||
| 1792 | Sys tem.Consol e.WriteLin e(c.FirstN ame + " " + c.LastNa me); | |||||
| 1793 | } | |||||
| 1794 | } | |||||
| 1795 | //</s nippetLINQ Examples34 > | |||||
| 1796 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1797 | // OU TPUT: | |||||
| 1798 | //Lis t of Conta ct Info us ing late b inding and the GetAt tributeVal ue method 2 | |||||
| 1799 | //=== ========== ========== ========== ===== | |||||
| 1800 | //Bri an Smith | |||||
| 1801 | //Dar ren Parker | |||||
| 1802 | //Ben Smith | |||||
| 1803 | //=== ========== ========== ========== ==== | |||||
| 1804 | Syste m.Console. WriteLine( ); | |||||
| 1805 | ||||||
| 1806 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1807 | // LNQ Using a late bindi ng join | |||||
| 1808 | // ** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** * | |||||
| 1809 | ||||||
| 1810 | Syste m.Console. WriteLine( ); | |||||
| 1811 | Syste m.Console. WriteLine( "List of I nfo using a late bin ding join" ); | |||||
| 1812 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 1813 | //<sn ippetLINQE xamples40> | |||||
| 1814 | using (Organiza tionServic eContext o rgSvcConte xt = new O rganizatio nServiceCo ntext(_ser viceProxy) ) | |||||
| 1815 | { | |||||
| 1816 | var query_dejo in = from c in orgSv cContext.C reateQuery ("contact" ) | |||||
| 1817 | join a in orgSv cContext.C reateQuery ("account" ) | |||||
| 1818 | on c[ "contactid "] equals a["primary contactid" ] | |||||
| 1819 | join l in orgSv cContext.C reateQuery ("lead") | |||||
| 1820 | on a[ "originati ngleadid"] equals l[ "leadid"] | |||||
| 1821 | where (string)c ["lastname "] != "Par ker" | |||||
| 1822 | selec t new { Co ntact = c, Account = a, Lead = l }; | |||||
| 1823 | fore ach (var c in query_ dejoin) | |||||
| 1824 | { | |||||
| 1825 | Sys tem.Consol e.WriteLin e(c.Accoun t.Attribut es["name"] + " " + | |||||
| 1826 | c. Contact.At tributes[" fullname"] + " " + c .Lead.Attr ibutes["le adid"]); | |||||
| 1827 | } | |||||
| 1828 | } | |||||
| 1829 | //</s nippetLINQ Examples40 > | |||||
| 1830 | Syste m.Console. WriteLine( "========= ========== ========== ========") ; | |||||
| 1831 | // OU TPUT: | |||||
| 1832 | //Lis t of Info using a la te binding join | |||||
| 1833 | //=== ========== ========== ========== ===== | |||||
| 1834 | //Con toso Ltd B rian Smith 9a8228af- 04a1-e011- b1b7-00155 dba3818 | |||||
| 1835 | //=== ========== ========== ========== ==== | |||||
| 1836 | Syste m.Console. WriteLine( ); | |||||
| 1837 | ||||||
| 1838 | #endr egion Late BoundExamp les | |||||
| 1839 | Delet eRequiredR ecords(pro mptforDele te); | |||||
| 1840 | } | |||||
| 1841 | } | |||||
| 1842 | ||||||
| 1843 | // Catc h any serv ice fault exceptions that Micr osoft Dyna mics CRM t hrows. | |||||
| 1844 | catch ( FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault>) | |||||
| 1845 | { | |||||
| 1846 | // You can handl e an excep tion here or pass it back to t he calling method. | |||||
| 1847 | throw; | |||||
| 1848 | } | |||||
| 1849 | } | |||||
| 1850 | ||||||
| 1851 | /// <sum mary> | |||||
| 1852 | /// Crea tes any en tity recor ds that th is sample requires. | |||||
| 1853 | /// </su mmary> | |||||
| 1854 | public v oid Create RequiredRe cords() | |||||
| 1855 | { | |||||
| 1856 | Contact contact1 = new Cont act | |||||
| 1857 | { | |||||
| 1858 | FirstN ame = "Col in", | |||||
| 1859 | LastNa me = "Wilc ox", | |||||
| 1860 | Addres s1_City = "Redmond", | |||||
| 1861 | Addres s1_StateOr Province = "WA", | |||||
| 1862 | Addres s1_PostalC ode = "980 52", | |||||
| 1863 | Annive rsary = ne w DateTime (2010, 3, 5), | |||||
| 1864 | Credit Limit = ne w Money(30 0), | |||||
| 1865 | Descri ption = "A lpine Ski House", | |||||
| 1866 | Status Code = new OptionSet Value(1), | |||||
| 1867 | Accoun tRoleCode = new Opti onSetValue (1), | |||||
| 1868 | Number OfChildren = 1, | |||||
| 1869 | Addres s1_Latitud e = 47.674 1667, | |||||
| 1870 | Addres s1_Longitu de = -122. 1202778, | |||||
| 1871 | Credit OnHold = f alse | |||||
| 1872 | }; | |||||
| 1873 | _contac tId1 = _se rviceProxy .Create(co ntact1); | |||||
| 1874 | ||||||
| 1875 | Console .Write("Cr eated a sa mple conta ct 1: {0}, ", contac t1.FirstNa me + " " + contact1. LastName); | |||||
| 1876 | ||||||
| 1877 | Contact contact2 = new Cont act | |||||
| 1878 | { | |||||
| 1879 | FirstN ame = "Bri an", | |||||
| 1880 | LastNa me = "Smit h", | |||||
| 1881 | Addres s1_City = "Bellevue" , | |||||
| 1882 | Family StatusCode = new Opt ionSetValu e(3), | |||||
| 1883 | Addres s1_StateOr Province = "WA", | |||||
| 1884 | Addres s1_PostalC ode = "980 08", | |||||
| 1885 | Annive rsary = ne w DateTime (2010, 4, 5), | |||||
| 1886 | Credit Limit = ne w Money(30 000), | |||||
| 1887 | Descri ption = "C oho Winery ", | |||||
| 1888 | Status Code = new OptionSet Value(1), | |||||
| 1889 | Accoun tRoleCode = new Opti onSetValue (2), | |||||
| 1890 | Number OfChildren = 2, | |||||
| 1891 | Addres s1_Latitud e = 47.610 5556, | |||||
| 1892 | Addres s1_Longitu de = -122. 1994444, | |||||
| 1893 | Credit OnHold = f alse | |||||
| 1894 | }; | |||||
| 1895 | _contac tId2 = _se rviceProxy .Create(co ntact2); | |||||
| 1896 | ||||||
| 1897 | Console .Write("Cr eated a sa mple conta ct 2: {0}, ", contac t2.FirstNa me + " " + contact2. LastName); | |||||
| 1898 | ||||||
| 1899 | Contact contact3 = new Cont act | |||||
| 1900 | { | |||||
| 1901 | FirstN ame = "Dar ren", | |||||
| 1902 | LastNa me = "Park er", | |||||
| 1903 | Addres s1_City = "Kirkland" , | |||||
| 1904 | Family StatusCode = new Opt ionSetValu e(3), | |||||
| 1905 | Addres s1_StateOr Province = "WA", | |||||
| 1906 | Addres s1_PostalC ode = "980 33", | |||||
| 1907 | Annive rsary = ne w DateTime (2010, 10, 5), | |||||
| 1908 | Credit Limit = ne w Money(10 000), | |||||
| 1909 | Descri ption = "C oho Winery ", | |||||
| 1910 | Status Code = new OptionSet Value(1), | |||||
| 1911 | Accoun tRoleCode = new Opti onSetValue (2), | |||||
| 1912 | Number OfChildren = 2, | |||||
| 1913 | Addres s1_Latitud e = 47.610 5556, | |||||
| 1914 | Addres s1_Longitu de = -122. 1994444, | |||||
| 1915 | Credit OnHold = f alse | |||||
| 1916 | }; | |||||
| 1917 | _contac tId3 = _se rviceProxy .Create(co ntact3); | |||||
| 1918 | ||||||
| 1919 | Console .Write("Cr eated a sa mple conta ct 3: {0}, ", contac t3.FirstNa me + " " + contact3. LastName); | |||||
| 1920 | ||||||
| 1921 | Contact contact4 = new Cont act | |||||
| 1922 | { | |||||
| 1923 | FirstN ame = "Ben ", | |||||
| 1924 | LastNa me = "Smit h", | |||||
| 1925 | Addres s1_City = "Kirkland" , | |||||
| 1926 | Family StatusCode = new Opt ionSetValu e(3), | |||||
| 1927 | Addres s1_StateOr Province = "WA", | |||||
| 1928 | Addres s1_PostalC ode = "980 33", | |||||
| 1929 | Annive rsary = ne w DateTime (2010, 7, 5), | |||||
| 1930 | Credit Limit = ne w Money(12 000), | |||||
| 1931 | Descri ption = "C oho Winery ", | |||||
| 1932 | Status Code = new OptionSet Value(1), | |||||
| 1933 | Accoun tRoleCode = new Opti onSetValue (2), | |||||
| 1934 | Number OfChildren = 2, | |||||
| 1935 | Addres s1_Latitud e = 47.610 5556, | |||||
| 1936 | Addres s1_Longitu de = -122. 1994444, | |||||
| 1937 | Credit OnHold = t rue | |||||
| 1938 | }; | |||||
| 1939 | _contac tId4 = _se rviceProxy .Create(co ntact4); | |||||
| 1940 | ||||||
| 1941 | Console .Write("Cr eated a sa mple conta ct 4: {0}, ", contac t4.FirstNa me + " " + contact4. LastName); | |||||
| 1942 | ||||||
| 1943 | Inciden t incident 1 = new In cident | |||||
| 1944 | { | |||||
| 1945 | Title = "Test Ca se 1", | |||||
| 1946 | Priori tyCode = n ew OptionS etValue(1) , // 1 = H igh | |||||
| 1947 | CaseOr iginCode = new Optio nSetValue( 1), // 1 = Phone | |||||
| 1948 | CaseTy peCode = n ew OptionS etValue(2) , // 2 = P roblem | |||||
| 1949 | Descri ption = "D escription for Test Case 1.", | |||||
| 1950 | Follow upBy = Dat eTime.Now. AddHours(3 .0), // fo llow-up in 3 hours | |||||
| 1951 | Custom erId = new EntityRef erence(Con tact.Entit yLogicalNa me, _conta ctId2) | |||||
| 1952 | }; | |||||
| 1953 | ||||||
| 1954 | _incide ntId1 = _s erviceProx y.Create(i ncident1); | |||||
| 1955 | ||||||
| 1956 | Console .Write("Cr eated a sa mple incid ent 1: {0} , ", incid ent1.Title ); | |||||
| 1957 | ||||||
| 1958 | Relatio nship rela tionship1 = new Rela tionship(" incident_c ustomer_co ntacts"); | |||||
| 1959 | EntityR eferenceCo llection r elatedEnti ties1 = ne w EntityRe ferenceCol lection(); | |||||
| 1960 | related Entities1. Add(new En tityRefere nce(Contac t.EntityLo gicalName, _contactI d1)); | |||||
| 1961 | _servic eProxy.Ass ociate(Inc ident.Enti tyLogicalN ame, _inci dentId1, r elationshi p1, relate dEntities1 ); | |||||
| 1962 | ||||||
| 1963 | Console .Write("Ad ded relati onship bet ween incid ent 1 and contact 1, "); | |||||
| 1964 | ||||||
| 1965 | ||||||
| 1966 | Account account1 = new Acco unt | |||||
| 1967 | { | |||||
| 1968 | Name = "Coho Win ery", | |||||
| 1969 | Addres s1_Name = "Coho Vine yard & Win ery", | |||||
| 1970 | Addres s1_City = "Redmond" | |||||
| 1971 | }; | |||||
| 1972 | _accoun tId1 = _se rviceProxy .Create(ac count1); | |||||
| 1973 | ||||||
| 1974 | Console .Write("Cr eated a sa mple accou nt 1: {0}, ", accoun t1.Name); | |||||
| 1975 | ||||||
| 1976 | Inciden t incident 2 = new In cident | |||||
| 1977 | { | |||||
| 1978 | Title = "Test Ca se 2", | |||||
| 1979 | Priori tyCode = n ew OptionS etValue(1) , // 1 = H igh | |||||
| 1980 | CaseOr iginCode = new Optio nSetValue( 1), // 1 = Phone | |||||
| 1981 | CaseTy peCode = n ew OptionS etValue(2) , // 2 = P roblem | |||||
| 1982 | Descri ption = "D escription for Sampl e Case 2." , | |||||
| 1983 | Follow upBy = Dat eTime.Now. AddHours(3 .0), // fo llow-up in 3 hours | |||||
| 1984 | Custom erId = new EntityRef erence(Con tact.Entit yLogicalNa me, _conta ctId1) | |||||
| 1985 | }; | |||||
| 1986 | ||||||
| 1987 | _incide ntId2 = _s erviceProx y.Create(i ncident2); | |||||
| 1988 | ||||||
| 1989 | Console .Write("Cr eated a sa mple incid ent 2: {0} , ", incid ent2.Title ); | |||||
| 1990 | ||||||
| 1991 | Relatio nship rela tionship2 = new Rela tionship(" incident_c ustomer_ac counts"); | |||||
| 1992 | EntityR eferenceCo llection r elatedEnti ties2 = ne w EntityRe ferenceCol lection(); | |||||
| 1993 | related Entities2. Add(new En tityRefere nce(Accoun t.EntityLo gicalName, _accountI d1)); | |||||
| 1994 | _servic eProxy.Ass ociate(Inc ident.Enti tyLogicalN ame, _inci dentId2, r elationshi p2, relate dEntities2 ); | |||||
| 1995 | ||||||
| 1996 | Console .Write("Ad ded relati onship bet ween incid ent 2 and account 1, "); | |||||
| 1997 | ||||||
| 1998 | Lead le ad = new L ead() | |||||
| 1999 | { | |||||
| 2000 | FirstN ame = "Dio go", | |||||
| 2001 | LastNa me = "Andr ade" | |||||
| 2002 | }; | |||||
| 2003 | _leadId = _servic eProxy.Cre ate(lead); | |||||
| 2004 | Console .Write("Cr eated a sa mple Lead: {0} ", le ad.FirstNa me + " " + lead.Last Name); | |||||
| 2005 | ||||||
| 2006 | Account account2 = new Acco unt | |||||
| 2007 | { | |||||
| 2008 | Name = "Contoso Ltd", | |||||
| 2009 | Parent AccountId = new Enti tyReferenc e(Account. EntityLogi calName, _ accountId1 ), | |||||
| 2010 | Addres s1_Name = "Contoso P harmaceuti cals", | |||||
| 2011 | Addres s1_City = "Redmond", | |||||
| 2012 | Origin atingLeadI d = new En tityRefere nce(Lead.E ntityLogic alName, _l eadId) | |||||
| 2013 | }; | |||||
| 2014 | _accoun tId2 = _se rviceProxy .Create(ac count2); | |||||
| 2015 | ||||||
| 2016 | Console .Write("Cr eated a sa mple accou nt 2: {0}, ", accoun t2.Name); | |||||
| 2017 | ||||||
| 2018 | Relatio nship rela tionship3 = new Rela tionship(" account_pr imary_cont act"); | |||||
| 2019 | EntityR eferenceCo llection r elatedEnti ties3 = ne w EntityRe ferenceCol lection(); | |||||
| 2020 | related Entities3. Add(new En tityRefere nce(Accoun t.EntityLo gicalName, _accountI d2)); | |||||
| 2021 | _servic eProxy.Ass ociate(Con tact.Entit yLogicalNa me, _conta ctId2, rel ationship3 , relatedE ntities3); | |||||
| 2022 | ||||||
| 2023 | Console .WriteLine ("Added re lationship between a ccount 2 a nd contact 2."); | |||||
| 2024 | } | |||||
| 2025 | /// <sum mary> | |||||
| 2026 | /// Dele tes any en tity recor ds that we re created for this sample. | |||||
| 2027 | /// <par am name="p rompt">Ind icates whe ther to pr ompt the u ser | |||||
| 2028 | /// to d elete the records cr eated in t his sample .</param> | |||||
| 2029 | /// </su mmary> | |||||
| 2030 | public v oid Delete RequiredRe cords(bool prompt) | |||||
| 2031 | { | |||||
| 2032 | // The three syst em users t hat were c reated by this sampl e will con tinue to | |||||
| 2033 | // exis t on your system bec ause syste m users ca nnot be de leted in M icrosoft | |||||
| 2034 | // Dyna mics CRM. They can only be en abled or d isabled. | |||||
| 2035 | ||||||
| 2036 | bool to BeDeleted = true; | |||||
| 2037 | ||||||
| 2038 | if (pro mpt) | |||||
| 2039 | { | |||||
| 2040 | // Ask the user if the cre ated entit ies should be delete d. | |||||
| 2041 | Consol e.Write("\ nDo you wa nt these e ntity reco rds delete d? (y/n) [ y]: "); | |||||
| 2042 | String answer = Console.Re adLine(); | |||||
| 2043 | if (an swer.Start sWith("y") || | |||||
| 2044 | an swer.Start sWith("Y") || | |||||
| 2045 | an swer == St ring.Empty ) | |||||
| 2046 | { | |||||
| 2047 | toBeD eleted = t rue; | |||||
| 2048 | } | |||||
| 2049 | else | |||||
| 2050 | { | |||||
| 2051 | toBeD eleted = f alse; | |||||
| 2052 | } | |||||
| 2053 | } | |||||
| 2054 | ||||||
| 2055 | if (toB eDeleted) | |||||
| 2056 | { | |||||
| 2057 | // Del ete all re cords crea ted in thi s sample. | |||||
| 2058 | _servi ceProxy.De lete(Accou nt.EntityL ogicalName , _account Id2); | |||||
| 2059 | _servi ceProxy.De lete(Incid ent.Entity LogicalNam e, _incide ntId2); | |||||
| 2060 | _servi ceProxy.De lete(Accou nt.EntityL ogicalName , _account Id1); | |||||
| 2061 | _servi ceProxy.De lete(Lead. EntityLogi calName, _ leadId); | |||||
| 2062 | _servi ceProxy.De lete(Incid ent.Entity LogicalNam e, _incide ntId1); | |||||
| 2063 | _servi ceProxy.De lete(Conta ct.EntityL ogicalName , _contact Id4); | |||||
| 2064 | _servi ceProxy.De lete(Conta ct.EntityL ogicalName , _contact Id3); | |||||
| 2065 | _servi ceProxy.De lete(Conta ct.EntityL ogicalName , _contact Id2); | |||||
| 2066 | _servi ceProxy.De lete(Conta ct.EntityL ogicalName , _contact Id1); | |||||
| 2067 | ||||||
| 2068 | Consol e.WriteLin e("Entity record(s) have been deleted.") ; | |||||
| 2069 | } | |||||
| 2070 | } | |||||
| 2071 | ||||||
| 2072 | #endregi on How To Sample Cod e | |||||
| 2073 | ||||||
| 2074 | #region Main metho d | |||||
| 2075 | ||||||
| 2076 | /// <sum mary> | |||||
| 2077 | /// Stan dard Main( ) method u sed by mos t SDK samp les. | |||||
| 2078 | /// </su mmary> | |||||
| 2079 | /// <par am name="a rgs"></par am> | |||||
| 2080 | static p ublic void Main(stri ng[] args) | |||||
| 2081 | { | |||||
| 2082 | try | |||||
| 2083 | { | |||||
| 2084 | // Obt ain the ta rget organ ization's Web addres s and clie nt logon | |||||
| 2085 | // cre dentials f rom the us er. | |||||
| 2086 | Server Connection serverCon nect = new ServerCon nection(); | |||||
| 2087 | Server Connection .Configura tion confi g = server Connect.Ge tServerCon figuration (); | |||||
| 2088 | ||||||
| 2089 | LINQ10 1 app = ne w LINQ101( ); | |||||
| 2090 | app.Ru n(config, true); | |||||
| 2091 | } | |||||
| 2092 | catch ( FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> e x) | |||||
| 2093 | { | |||||
| 2094 | Consol e.WriteLin e("The app lication t erminated with an er ror."); | |||||
| 2095 | Consol e.WriteLin e("Timesta mp: {0}", ex.Detail. Timestamp) ; | |||||
| 2096 | Consol e.WriteLin e("Code: { 0}", ex.De tail.Error Code); | |||||
| 2097 | Consol e.WriteLin e("Message : {0}", ex .Detail.Me ssage); | |||||
| 2098 | Consol e.WriteLin e("Plugin Trace: {0} ", ex.Deta il.TraceTe xt); | |||||
| 2099 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 2100 | nu ll == ex.D etail.Inne rFault ? " No Inner F ault" : "H as Inner F ault"); | |||||
| 2101 | } | |||||
| 2102 | catch ( System.Tim eoutExcept ion ex) | |||||
| 2103 | { | |||||
| 2104 | Consol e.WriteLin e("The app lication t erminated with an er ror."); | |||||
| 2105 | Consol e.WriteLin e("Message : {0}", ex .Message); | |||||
| 2106 | Consol e.WriteLin e("Stack T race: {0}" , ex.Stack Trace); | |||||
| 2107 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 2108 | nu ll == ex.I nnerExcept ion.Messag e ? "No In ner Fault" : ex.Inne rException .Message); | |||||
| 2109 | } | |||||
| 2110 | catch ( System.Exc eption ex) | |||||
| 2111 | { | |||||
| 2112 | Consol e.WriteLin e("The app lication t erminated with an er ror."); | |||||
| 2113 | Consol e.WriteLin e(ex.Messa ge); | |||||
| 2114 | ||||||
| 2115 | // Dis play the d etails of the inner exception. | |||||
| 2116 | if (ex .InnerExce ption != n ull) | |||||
| 2117 | { | |||||
| 2118 | Conso le.WriteLi ne(ex.Inne rException .Message); | |||||
| 2119 | ||||||
| 2120 | Fault Exception< Microsoft. Xrm.Sdk.Or ganization ServiceFau lt> fe = e x.InnerExc eption | |||||
| 2121 | a s FaultExc eption<Mic rosoft.Xrm .Sdk.Organ izationSer viceFault> ; | |||||
| 2122 | if (f e != null) | |||||
| 2123 | { | |||||
| 2124 | Cons ole.WriteL ine("Times tamp: {0}" , fe.Detai l.Timestam p); | |||||
| 2125 | Cons ole.WriteL ine("Code: {0}", fe. Detail.Err orCode); | |||||
| 2126 | Cons ole.WriteL ine("Messa ge: {0}", fe.Detail. Message); | |||||
| 2127 | Cons ole.WriteL ine("Plugi n Trace: { 0}", fe.De tail.Trace Text); | |||||
| 2128 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 2129 | null == fe .Detail.In nerFault ? "No Inner Fault" : "Has Inner Fault"); | |||||
| 2130 | } | |||||
| 2131 | } | |||||
| 2132 | } | |||||
| 2133 | // Addi tonal exce ptions to catch: Sec urityToken Validation Exception, ExpiredSe curityToke nException , | |||||
| 2134 | // Secu rityAccess DeniedExce ption, Mes sageSecuri tyExceptio n, and Sec urityNegot iationExce ption. | |||||
| 2135 | ||||||
| 2136 | finally | |||||
| 2137 | { | |||||
| 2138 | Consol e.WriteLin e("Press < Enter> to exit."); | |||||
| 2139 | Consol e.ReadLine (); | |||||
| 2140 | } | |||||
| 2141 | } | |||||
| 2142 | #endregi on Main me thod | |||||
| 2143 | } | |||||
| 2144 | } | |||||
| 2145 | //</snippe tLINQExamp les> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.