[{"data":1,"prerenderedAt":585},["ShallowReactive",2],{"blog:2009:linq-to-sql-tips-and-tricks-2":3,"blogMore-Development":571,"comments-linq-to-sql-tips-and-tricks-2":584},{"id":4,"title":5,"body":6,"category":552,"commentCount":121,"date":553,"description":12,"excerpt":554,"extension":555,"filenames":556,"hidden":557,"image":556,"meta":558,"minutes":105,"navigation":559,"path":560,"seo":561,"showCategory":556,"stem":562,"tags":563,"updated":556,"url":568,"wordCount":569,"__hash__":570},"content\u002Fblog\u002F2009\u002Flinq-to-sql-tips-and-tricks-2.md","LINQ to SQL tips and tricks #2",{"type":7,"value":8,"toc":547},"minimark",[9,13,18,21,24,247,251,258,261,279,285,289,292,303,483,486,522,525,528,537,543],[10,11,12],"p",{},"A few more useful and lesser-known tips for using LINQ to SQL.",[14,15,17],"h2",{"id":16},"take-full-control-of-the-tsql","Take full control of the TSQL",[10,19,20],{},"There are times when LINQ to SQL refuses to cook up the TSQL you wanted either because it doesn’t support the feature or because it has a different idea of what makes an optimal query.",[10,22,23],{},"In either case, the Translate method allows you to deliver your own TSQL to LINQ to SQL, to execute, with materialization and identity mapping still honored. For example:",[25,26,31],"pre",{"className":27,"code":28,"language":29,"meta":30,"style":30},"language-csharp shiki shiki-themes everforest-light dracula","var db = new PeopleContext();\nif (db.Connection.State == System.Data.ConnectionState.Closed)\n    db.Connection.Open();\nvar cmd = db.GetCommand(db.Persons.Where(p => p.CountryID == 1));\ncmd.CommandText = cmd.CommandText.Replace(\"[People] AS [t0]\", \"[People] AS [t0] WITH (NOLOCK)\");\nvar results = db.Translate\u003CPerson>(cmd.ExecuteReader());\n","csharp","",[32,33,34,62,103,119,170,217],"code",{"__ignoreMap":30},[35,36,39,43,47,51,55,59],"span",{"class":37,"line":38},"line",1,[35,40,42],{"class":41},"sXAHl","var",[35,44,46],{"class":45},"s6Vpi"," db ",[35,48,50],{"class":49},"s9HRq","=",[35,52,54],{"class":53},"smiwp"," new",[35,56,58],{"class":57},"snuxY"," PeopleContext",[35,60,61],{"class":45},"();\n",[35,63,65,68,71,75,78,81,84,87,90,92,95,97,100],{"class":37,"line":64},2,[35,66,67],{"class":53},"if",[35,69,70],{"class":45}," (db.",[35,72,74],{"class":73},"sSKRk","Connection",[35,76,77],{"class":45},".",[35,79,80],{"class":73},"State",[35,82,83],{"class":49}," ==",[35,85,86],{"class":45}," System.",[35,88,89],{"class":73},"Data",[35,91,77],{"class":45},[35,93,94],{"class":73},"ConnectionState",[35,96,77],{"class":45},[35,98,99],{"class":73},"Closed",[35,101,102],{"class":45},")\n",[35,104,106,109,111,113,117],{"class":37,"line":105},3,[35,107,108],{"class":45},"    db.",[35,110,74],{"class":73},[35,112,77],{"class":45},[35,114,116],{"class":115},"sS4Kt","Open",[35,118,61],{"class":45},[35,120,122,124,127,129,132,135,138,141,143,146,149,152,155,158,161,163,167],{"class":37,"line":121},4,[35,123,42],{"class":41},[35,125,126],{"class":45}," cmd ",[35,128,50],{"class":49},[35,130,131],{"class":45}," db.",[35,133,134],{"class":115},"GetCommand",[35,136,137],{"class":45},"(db.",[35,139,140],{"class":73},"Persons",[35,142,77],{"class":45},[35,144,145],{"class":115},"Where",[35,147,148],{"class":45},"(",[35,150,10],{"class":151},"s7cAX",[35,153,154],{"class":49}," =>",[35,156,157],{"class":45}," p.",[35,159,160],{"class":73},"CountryID",[35,162,83],{"class":49},[35,164,166],{"class":165},"s3Ipq"," 1",[35,168,169],{"class":45},"));\n",[35,171,173,176,179,182,185,187,189,192,194,198,202,204,207,209,212,214],{"class":37,"line":172},5,[35,174,175],{"class":45},"cmd.",[35,177,178],{"class":73},"CommandText",[35,180,181],{"class":49}," =",[35,183,184],{"class":45}," cmd.",[35,186,178],{"class":73},[35,188,77],{"class":45},[35,190,191],{"class":115},"Replace",[35,193,148],{"class":45},[35,195,197],{"class":196},"sciFF","\"",[35,199,201],{"class":200},"sJQOs","[People] AS [t0]",[35,203,197],{"class":196},[35,205,206],{"class":45},", ",[35,208,197],{"class":196},[35,210,211],{"class":200},"[People] AS [t0] WITH (NOLOCK)",[35,213,197],{"class":196},[35,215,216],{"class":45},");\n",[35,218,220,222,225,227,229,232,235,238,241,244],{"class":37,"line":219},6,[35,221,42],{"class":41},[35,223,224],{"class":45}," results ",[35,226,50],{"class":49},[35,228,131],{"class":45},[35,230,231],{"class":115},"Translate",[35,233,234],{"class":45},"\u003C",[35,236,237],{"class":57},"Person",[35,239,240],{"class":45},">(cmd.",[35,242,243],{"class":115},"ExecuteReader",[35,245,246],{"class":45},"());\n",[14,248,250],{"id":249},"complex-stored-procedures","Complex stored procedures",[10,252,253,254,257],{},"When working with stored procedures, the LINQ to SQL designer and SQLMetal tools need to figure out what the return type is. To do this without actually running the stored procedure, they use the ",[32,255,256],{},"SET FMTONLY"," command so that SQL Server parses the stored procedure but does not execute it.",[10,259,260],{},"Unfortunately, this parsing does not extend to dynamic SQL or temporary tables, so you must change the return type from the scalar integer to one of the known entity types by hand. You could use the following command at the start to let it run, regardless of the subsequent warning.",[25,262,266],{"className":263,"code":264,"language":265,"meta":30,"style":30},"language-sql shiki shiki-themes everforest-light dracula","SET FMTONLY OFF\n","sql",[32,267,268],{"__ignoreMap":30},[35,269,270,273,276],{"class":37,"line":38},[35,271,272],{"class":53},"SET",[35,274,275],{"class":53}," FMTONLY",[35,277,278],{"class":53}," OFF\n",[280,281,282],"blockquote",{},[10,283,284],{},"If your stored procedure can not safely handle calls at any time with null parameters, set the return type by hand instead.",[14,286,288],{"id":287},"cloning-an-entity","Cloning an entity",[10,290,291],{},"There are many reasons you might want to clone an entity: you may want to create many similar ones, you could want to keep it around longer than the DataContext it came from. Whatever your reason implementing a Clone method can be a pain but taking advantage of the DataContractSerializer can make light work of this providing your DBML is set to enable serialization.",[10,293,294,295,302],{},"If you use discriminator subclassing, you need to either ensure your type is cast to its concrete type or use my ",[296,297,301],"a",{"href":298,"rel":299},"https:\u002F\u002Fl2st4.codeplex.com",[300],"nofollow","L2ST4 templates"," for now as .NET 3.5 SP1 doesn’t emit the necessary KnownType attributes to make this automatically happen (fixed in .NET 4.0). Add a simple method to serialize in-memory like this:",[25,304,306],{"className":27,"code":305,"language":29,"meta":30,"style":30},"public static T Clone\u003CT>(T source) {\n    var dcs = new System.Runtime.Serialization.DataContractSerializer(typeof(T));\n    using (var ms = new System.IO.MemoryStream()) {\n        dcs.WriteObject(ms, source);\n        ms.Seek(0, System.IO.SeekOrigin.Begin);\n        return (T)dcs.ReadObject(ms);\n    }\n}\n",[32,307,308,339,380,412,423,453,471,477],{"__ignoreMap":30},[35,309,310,313,316,319,322,324,328,331,333,336],{"class":37,"line":38},[35,311,312],{"class":49},"public",[35,314,315],{"class":49}," static",[35,317,318],{"class":57}," T",[35,320,321],{"class":115}," Clone",[35,323,234],{"class":45},[35,325,327],{"class":326},"sAO9U","T",[35,329,330],{"class":45},">(",[35,332,327],{"class":57},[35,334,335],{"class":151}," source",[35,337,338],{"class":45},") {\n",[35,340,341,344,347,349,351,354,356,359,361,364,366,369,371,374,376,378],{"class":37,"line":64},[35,342,343],{"class":41},"    var",[35,345,346],{"class":45}," dcs ",[35,348,50],{"class":49},[35,350,54],{"class":53},[35,352,353],{"class":57}," System",[35,355,77],{"class":45},[35,357,358],{"class":57},"Runtime",[35,360,77],{"class":45},[35,362,363],{"class":57},"Serialization",[35,365,77],{"class":45},[35,367,368],{"class":57},"DataContractSerializer",[35,370,148],{"class":45},[35,372,373],{"class":53},"typeof",[35,375,148],{"class":45},[35,377,327],{"class":57},[35,379,169],{"class":45},[35,381,382,385,388,390,393,395,397,399,401,404,406,409],{"class":37,"line":105},[35,383,384],{"class":53},"    using",[35,386,387],{"class":45}," (",[35,389,42],{"class":41},[35,391,392],{"class":45}," ms ",[35,394,50],{"class":49},[35,396,54],{"class":53},[35,398,353],{"class":57},[35,400,77],{"class":45},[35,402,403],{"class":57},"IO",[35,405,77],{"class":45},[35,407,408],{"class":57},"MemoryStream",[35,410,411],{"class":45},"()) {\n",[35,413,414,417,420],{"class":37,"line":121},[35,415,416],{"class":45},"        dcs.",[35,418,419],{"class":115},"WriteObject",[35,421,422],{"class":45},"(ms, source);\n",[35,424,425,428,431,433,436,439,441,443,446,448,451],{"class":37,"line":172},[35,426,427],{"class":45},"        ms.",[35,429,430],{"class":115},"Seek",[35,432,148],{"class":45},[35,434,435],{"class":165},"0",[35,437,438],{"class":45},", System.",[35,440,403],{"class":73},[35,442,77],{"class":45},[35,444,445],{"class":73},"SeekOrigin",[35,447,77],{"class":45},[35,449,450],{"class":73},"Begin",[35,452,216],{"class":45},[35,454,455,458,460,462,465,468],{"class":37,"line":219},[35,456,457],{"class":53},"        return",[35,459,387],{"class":45},[35,461,327],{"class":57},[35,463,464],{"class":45},")dcs.",[35,466,467],{"class":115},"ReadObject",[35,469,470],{"class":45},"(ms);\n",[35,472,474],{"class":37,"line":473},7,[35,475,476],{"class":45},"    }\n",[35,478,480],{"class":37,"line":479},8,[35,481,482],{"class":45},"}\n",[10,484,485],{},"And then to clone:",[25,487,489],{"className":27,"code":488,"language":29,"meta":30,"style":30},"var source = myQuery.First();\nvar cloned = Clone(source);\n",[32,490,491,508],{"__ignoreMap":30},[35,492,493,495,498,500,503,506],{"class":37,"line":38},[35,494,42],{"class":41},[35,496,497],{"class":45}," source ",[35,499,50],{"class":49},[35,501,502],{"class":45}," myQuery.",[35,504,505],{"class":115},"First",[35,507,61],{"class":45},[35,509,510,512,515,517,519],{"class":37,"line":64},[35,511,42],{"class":41},[35,513,514],{"class":45}," cloned ",[35,516,50],{"class":49},[35,518,321],{"class":115},[35,520,521],{"class":45},"(source);\n",[10,523,524],{},"Be aware that this comes with a little overhead in the serialization and de-serialization process.",[10,526,527],{},"If this is a problem for you, why not grab the templates and make your entities implement ICloneable!",[280,529,530],{},[10,531,532,533],{},"Check out ",[296,534,536],{"href":535},"\u002Fblog\u002F2010\u002Flinq-to-sql-tips-and-tricks-3\u002F","part 3 of LINQ to SQL tips",[10,538,539],{},[540,541,542],"em",{},"[)amien",[544,545,546],"style",{},"html pre.shiki code .sXAHl, html code.shiki .sXAHl{--shiki-default:#3A94C5;--shiki-dark:#FF79C6}html pre.shiki code .s6Vpi, html code.shiki .s6Vpi{--shiki-default:#5C6A72;--shiki-dark:#F8F8F2}html pre.shiki code .s9HRq, html code.shiki .s9HRq{--shiki-default:#F57D26;--shiki-dark:#FF79C6}html pre.shiki code .smiwp, html code.shiki .smiwp{--shiki-default:#F85552;--shiki-dark:#FF79C6}html pre.shiki code .snuxY, html code.shiki .snuxY{--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic}html pre.shiki code .sSKRk, html code.shiki .sSKRk{--shiki-default:#35A77C;--shiki-dark:#F8F8F2}html pre.shiki code .sS4Kt, html code.shiki .sS4Kt{--shiki-default:#8DA101;--shiki-dark:#50FA7B}html pre.shiki code .s7cAX, html code.shiki .s7cAX{--shiki-default:#5C6A72;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic}html pre.shiki code .s3Ipq, html code.shiki .s3Ipq{--shiki-default:#DF69BA;--shiki-dark:#BD93F9}html pre.shiki code .sciFF, html code.shiki .sciFF{--shiki-default:#8DA101;--shiki-dark:#E9F284}html pre.shiki code .sJQOs, html code.shiki .sJQOs{--shiki-default:#8DA101;--shiki-dark:#F1FA8C}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sAO9U, html code.shiki .sAO9U{--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic}",{"title":30,"searchDepth":64,"depth":64,"links":548},[549,550,551],{"id":16,"depth":64,"text":17},{"id":249,"depth":64,"text":250},{"id":287,"depth":64,"text":288},"Development","2009-04-12T23:21:05+00:00","[object Object]","md",null,false,{},true,"\u002Fblog\u002F2009\u002Flinq-to-sql-tips-and-tricks-2",{"title":5,"description":12},"blog\u002F2009\u002Flinq-to-sql-tips-and-tricks-2",[564,565,566,567],".NET","LINQ","C#","SQL","\u002Fblog\u002F2009\u002Flinq-to-sql-tips-and-tricks-2\u002F",631,"yWzMieWwmDsuJ4PYH3xYEi7HdCCuVy0z3kFtfLTfuvA",[572,576,580],{"title":573,"date":574,"url":575},"Transactions in the MongoDB EF Core Provider","2025-10-25","\u002Fblog\u002F2025\u002Fmongodb-explicit-transactions\u002F",{"title":577,"date":578,"url":579},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","\u002Fblog\u002F2025\u002Fmongodb-queryable-encryption\u002F",{"title":581,"date":582,"url":583},"Lazy Loading with EF Core Proxies","2025-04-02","\u002Fblog\u002F2025\u002Fef-proxies\u002F",[],1780900527621]