LINQ Project General ForumAsk questions about the LINQ Project. The LINQ Project delivers Language Integrated Query (LINQ) for objects, relational, XML, and other forms of data.© 2009 Microsoft Corporation. All rights reserved.Sun, 29 Nov 2009 00:54:38 Z4ce19f39-4d93-4520-8516-fe9cf777cd3ahttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/af433a78-ce76-4394-b79b-dea05141a44ahttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/af433a78-ce76-4394-b79b-dea05141a44aZeeshan Janhttp://social.msdn.microsoft.com/Profile/en-US/?user=Zeeshan%20Janusing LINQ to query custom collection <p>I have a question for the LINQ lovers. Consider that I have a custom collection Products class which in turn is a collection of a class Product.<br/>Products<br/>  Product</p> <p>Till here is fine, If i extend it further and have Product as a collection of a class named Variant. Thus now I have:<br/>Products<br/> Product<br/>  Variant</p> <p>Now I finally have the three classes Products , Product and Variant. Products is the collection of Product class , Product is the collection of Variant clss.</p> <p>If I have a variantID e.g VAR-12 and I would like to use my favourite LINQ to query the Products collection to get the Product which has the variant id as VAR-12.</p> <p>are you ready :)))</p><hr class="sig">zeeshan [ visit my forum: www.code4project.com ]Fri, 27 Nov 2009 05:40:12 Z2009-11-29T00:54:38Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/2c1c66d0-cf03-4eb3-828b-3b553fb2717ahttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/2c1c66d0-cf03-4eb3-828b-3b553fb2717aJuriGhttp://social.msdn.microsoft.com/Profile/en-US/?user=JuriGLINQ2 SQL for a n-tier Application?Hi there,<br/> <br/> i want to use LINQ2SQL for an O/R Mapper in an n-Tier Application. <br/> Now i am little bit scarried because i have to maintain a reference to an DataContext.<br/> Is there a common guideline how to use LINQ2SQL for n-tier applications?<br/>Thu, 26 Nov 2009 08:20:37 Z2009-11-29T00:50:53Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/287de315-710b-4ffb-aad4-e461600a3946http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/287de315-710b-4ffb-aad4-e461600a3946pkellnerhttp://social.msdn.microsoft.com/Profile/en-US/?user=pkellnerSync Problems/Question with LINQ2SQLI'm trying to understand the behavior of having two separate processes (different computers, same sqlserver 2008) update a record in a table.  I can't seem to figure out the logic behind when the update is available to the other process.  that is,<br/><br/>Computer A writes record<br/>Computer B reads just written record (does not always see update).<br/><br/>I've noticed that when the application exits that wrote the record on Computer A, the record becomes visible.  Obviously, the behavior is consistent, I'm just struggling with with what that consistency is.  I've played with shared datacontext, not shared datacontext and still don't get it.<br/><br/>Thanks,<br/><br/>(and, I'm not using transactions explicityly)<hr class="sig">Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider Sat, 28 Nov 2009 23:21:26 Z2009-11-28T23:21:26Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/34e05602-4069-44b9-b532-5ae409bddd05http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/34e05602-4069-44b9-b532-5ae409bddd05SunilThkhttp://social.msdn.microsoft.com/Profile/en-US/?user=SunilThkHow is it possible to call System.Xml.Linq.XElement.Elements method with string parameter even though there is no overload which takes string as parameter<p>Hi All,<br/>   Can any one please clarify on the following:-<br/>System.Xml.Linq.XElement class has a method - public IEnumerable&lt;XElement&gt; Elements(XName name)</p> <p>  which takes a parameter of type XName, however its possible to call the Elements method<br/>  with string parameter, even though there is no overload of this method which takes a string<br/>  parameter, then how is it possible?<br/><br/>Thanks.</p>Sat, 28 Nov 2009 08:12:40 Z2009-11-28T12:05:16Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/14dfe195-760e-4d62-8949-aac3e2858f54http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/14dfe195-760e-4d62-8949-aac3e2858f54bjransonhttp://social.msdn.microsoft.com/Profile/en-US/?user=bjransonGetting null references on subordinate object references in LINQ query resultsI'm very new to LINQ, so apologies if my nomenclature is out. However, I'm very experienced in SQL and C#.<br/><br/>I have two tables in an SQL relational database, tblUsers and tblPosts, regarding a blogging software piece I'm creating.<br/><br/>tblPosts has a column called &quot;UserID&quot;, which refers to the User that created the Post.<br/><br/>I have provided a foreign key reference in SQL Server, that links the &quot;UserID&quot; in tblPosts, to the Primary &quot;UserID&quot; in tblUsers.<br/><br/>When creating my .edmx file in .NET 3.5 in VS2008, this relationship carries over correctly on the Designer diagram.<br/><br/>I translated tblPosts to &quot;Post&quot; and tblUsers to &quot;User&quot; on the .edmx Designed to help make my C# code more sensible to read.<br/><br/>Now, the problem I'm getting is that when I try to reference the &quot;User&quot; object &quot;embedded&quot; into a &quot;Post&quot; object after querying or running a foreach, I get a null on the User object. I cannot reference the User referenced by the Post, and I believe that I am supposed to (or why would LINQ make it possible to have such a convenient reference?)<br/><br/>Here's a code Example:<span style="color:#0000ff;font-size:x-small"></span> <p><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">NearstarsEntities</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> db </span></span><span style="font-size:x-small">=</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> </span></span><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small">new</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> </span></span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">NearstarsEntities</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">() ; </span></span> </p> <p><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small">foreach</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> (</span></span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">Post</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> p </span></span><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small">in</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> db</span></span><span style="font-size:x-small">.</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">PostSet)<br/>{<br/></span></span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">           User</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> u </span></span><span style="font-size:x-small">=</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> p</span></span><span style="font-size:x-small">.</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">User;  //&lt;---- p is a valid object with valid data, but p.User is NULL!!!!<br/></span></span></p> <br/>I've even tried doing a join, to see if that's the problem, but this also gives me a NULL User object:<span style="color:#0000ff;font-size:x-small"><span style="font-family:;color:#0000ff;font-size:x-small"> </span></span> <p><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">NearstarsEntities</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> db </span></span><span style="font-size:x-small">=</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> </span></span><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small">new</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> </span></span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">NearstarsEntities</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">() ;<br/><br/></span></span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">IQueryable</span></span><span style="font-size:x-small">&lt;</span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">Post</span></span><span style="font-size:x-small">&gt;</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> ps </span></span><span style="font-size:x-small">=</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> </span></span><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small">from</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> posts </span></span><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small">in</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> db</span></span><span style="font-size:x-small">.</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">PostSet<br/></span></span><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small"> join</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> user </span></span><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small">in</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> db</span></span><span style="font-size:x-small">.</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">UserSet<br/></span></span><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small"> on</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> posts</span></span><span style="font-size:x-small">.</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">User</span></span><span style="font-size:x-small">.</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">UserID </span></span><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small">equals</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> user</span></span><span style="font-size:x-small">.</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">UserID <br/></span></span><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small"> select</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> posts ;<span style="font-family:;color:#0000ff;font-size:x-small"><span style="font-family:;color:#0000ff;font-size:x-small"> </span></span></span></span> </p> <p><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small">foreach</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> (</span></span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">Post</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> p </span></span><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small">in</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> ps){<span style="font-family:;color:#0000ff;font-size:x-small"><span style="font-family:;color:#0000ff;font-size:x-small"> </span></span></span></span> </p> <p><span style="color:#ff00ff;font-size:x-small"><span style="color:#ff00ff;font-size:x-small">string</span></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> sUsername </span></span><span style="font-size:x-small">=</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small"> p</span></span><span style="font-size:x-small">.</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">User</span></span><span style="font-size:x-small">.</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">Username;  //&lt;---- p is a valid object with valid data, but p.User is NULL!!!!</span></span></p> Can anyone see what it is I'm doing wrong, or what I'm missing out?<br/>Sat, 28 Nov 2009 01:34:16 Z2009-11-28T06:00:44Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/e40ceef5-aa8f-4442-bedf-0491d2416af5http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/e40ceef5-aa8f-4442-bedf-0491d2416af5Exsurgohttp://social.msdn.microsoft.com/Profile/en-US/?user=ExsurgoIncluding only specific properties in LINQ-to-SQL joins<p>I'm running the following query which is joing the Task table to the User table:</p> <pre lang="x-c#">DataLoadOptions options = new DataLoadOptions(); options.LoadWith&lt;Task&gt;(o =&gt; o.User); Domain.LoadOptions = options; IQueryable&lt;Task&gt; query = from o in Domain.Tasks select o;</pre> This of course eagerly loads all Tasks and all joined Users.    How can I join only a single property, such as User.Username?<br/><br/>I've tried &quot;options.LoadWith&lt;Task&gt;(o =&gt; o.User.Username);&quot; But this throws an exception.  I also need to make sure the query returns Task objects though instead of anonymous objects.<br/>Thu, 26 Nov 2009 00:24:48 Z2009-11-28T02:16:36Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/67857f81-fa75-451d-9250-d79271bff589http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/67857f81-fa75-451d-9250-d79271bff589yilmaz_neohttp://social.msdn.microsoft.com/Profile/en-US/?user=yilmaz_neoHow to execute a LINQ Query that is grouped by multiple columns<div>I have the following query. nssRecords is a list of objects       </div> <div><br/></div> <div>     var userGroupView = from record in nssRecords</div> <div>                                orderby record.user_id</div> <div>                                group record by new { record.user_id, record.import_dt } into userGroup</div> <div>                                select userGroup;</div> <div><br/></div> <div>when try to iterate through the query results, i get a  type mismatch error.</div> <div><br/></div> <div>            foreach (IGrouping&lt;string, NssRecord&gt; productGroup in userGroupView)</div> <div>            {</div> <div>                foreach (var row in productGroup)</div> <div>                {</div> <div>                    output.Text += row.user_id + &quot;----&quot; + row.import_dt + &quot;----&quot;;</div> <div>                }</div> <div>            }</div> <div><br/></div> <div>I get the following error:</div> <div><br/></div> <div>Unable to cast object of type 'Grouping[&lt;&gt;f__AnonymousType0`2[System.String,System.String],Manual368ImportingApp.NssRecord]' to type 'System.Linq.IGrouping`2[System.String,Manual368ImportingApp.NssRecord]'.</div> <div><br/></div> <div>If anyone knows how I can fix it, please let me know. This drives me crazy. </div> <div><br/></div> <div><br/></div> <div><br/></div> <div><br/></div>Fri, 27 Nov 2009 06:49:41 Z2009-11-27T14:04:58Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/7e6efdea-f03c-4e70-b72f-fcd6921d0207http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/7e6efdea-f03c-4e70-b72f-fcd6921d0207nasumhttp://social.msdn.microsoft.com/Profile/en-US/?user=nasumLambda Expression on List CollectionHi,<br/><br/>I am new to Linq trying to learn good features. Could please help me to get an example on usage of <br/><br/>Expression&lt;Func&lt;List&lt;class&gt;,int, int&gt;&gt; expression, Here I need to do an operation on List&lt;Class&gt; at index 'Int' (second param).<br/><br/>Help let me know if you have any good info on this context as well.<br/><br/>Regards,<hr class="sig">sreedharFri, 27 Nov 2009 10:24:02 Z2009-11-27T10:24:03Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/1c56543e-54e4-4f51-b45e-abb7f6491ddbhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/1c56543e-54e4-4f51-b45e-abb7f6491ddbKevin Peterson.http://social.msdn.microsoft.com/Profile/en-US/?user=Kevin%20Peterson.Memory issue with LINQhi <div><br/></div> <div>i have just start to work with LINQ. When i create a dataContext, it load all the table with the data to the memory at once, before even i start to manipulate the data. </div> <div><br/></div> <div>this is my code...</div> <div><br/></div> <div>myDataContext db = new myDataContext();</div> <div><br/></div> <div>in the debug mode, when i look into &quot;db&quot;, it shows all the table with all the data it self. </div> <div><br/></div> <div>if i have more than 20 tables with more than 1000 records each, what will happed to the memory. </div> <div><br/></div> <div>Or do i miss anything, or is there are anyway to manage the memory? Please help me on this issue?</div> <div><br/></div> <div>/kevin</div>Thu, 26 Nov 2009 06:23:43 Z2009-11-26T08:26:48Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/64dd035f-7bf0-4e43-b0b0-aa6f19ac04c2http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/64dd035f-7bf0-4e43-b0b0-aa6f19ac04c2darthghandihttp://social.msdn.microsoft.com/Profile/en-US/?user=darthghandiPrincipalContext.ValidateCredentials always true on XP<p align=left><font face=Arial size=2></font> </p> <p align=left>I was messing around with .net 3.5's new System.DirectoryServices.AccountManagement when I ran into a problem with PrincipalContext.ValidateCredentials.  I am able to validate a username and password on my laptop (windows XP SP2 joined to a domain), but when I run the same code on my desktop (Windows XP SP2 member of a workgroup) I am able to type in anything for the username and anything for the password (as long as I am authenticating against the local machine) and it will validate.  Am I doing something wrong here?  Here is the code:</p><font color="#0000ff" size=2> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Block</span></div><font color="#0000ff" size=2> <p align=left>sing</font><font size=2> System;</p></font><font color="#0000ff" size=2> <p align=left>using</font><font size=2> System.Collections.Generic;</p></font><font color="#0000ff" size=2> <p align=left>using</font><font size=2> System.Linq;</p></font><font color="#0000ff" size=2> <p align=left>using</font><font size=2> System.Text;</p></font><font color="#0000ff" size=2> <p align=left>using</font><font size=2> System.DirectoryServices.AccountManagement;</p></font><font color="#0000ff" size=2> <p align=left>namespace</font><font size=2> TryAccount</p> <p align=left>{</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left></font><font color="#0000ff" size=2>class</font><font size=2> </font><font color="#2b91af" size=2>Program</p></font><font size=2> <p align=left>{</p> <p align=left></font><font color="#0000ff" size=2>static</font><font size=2> </font><font color="#2b91af" size=2>PrincipalContext</font><font size=2> m_principalContext;</p> <p align=left></font><font color="#0000ff" size=2>static</font><font size=2> </font><font color="#2b91af" size=2>UserPrincipal</font><font size=2> m_userPrincipal;</p> <p align=left></font><font color="#0000ff" size=2>public</font><font size=2> </font><font color="#0000ff" size=2>static</font><font size=2> </font><font color="#0000ff" size=2>bool</font><font size=2> AuthenticateUser(</font><font color="#0000ff" size=2>string</font><font size=2> username, </font><font color="#0000ff" size=2>string</font><font size=2> password)</p> <p align=left>{</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left></font><font color="#0000ff" size=2>bool</font><font size=2> isAuthentic = </font><font color="#0000ff" size=2>false</font><font size=2>;</p> <p align=left></font><font color="#0000ff" size=2>try</p></font><font size=2> <p align=left>{</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left></font><font color="#0000ff" size=2>if</font><font size=2> (username.Contains(</font><font color="#a31515" size=2>&quot;\\&quot;</font><font size=2>))</p> <p align=left>{</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left></font><font color="#0000ff" size=2>string</font><font size=2>[] holdName = username.Split(</font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#0000ff" size=2>char</font><font size=2>[] { </font><font color="#a31515" size=2>'\\'</font><font size=2> });</p> <p align=left>m_principalContext = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>PrincipalContext</font><font size=2>(</font><font color="#2b91af" size=2>ContextType</font><font size=2>.Domain, holdName[0]);</p> <p align=left>isAuthentic = m_principalContext.ValidateCredentials(holdName[1], password, </font><font color="#2b91af" size=2>ContextOptions</font><font size=2>.Negotiate);</p></blockquote> <p align=left>}</p> <p align=left></font><font color="#0000ff" size=2>else</p></font><font size=2> <p align=left>{</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>m_principalContext = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>PrincipalContext</font><font size=2>(</font><font color="#2b91af" size=2>ContextType</font><font size=2>.Machine);</p> <p align=left>isAuthentic = m_principalContext.ValidateCredentials(username, password, </font><font color="#2b91af" size=2>ContextOptions</font><font size=2>.Negotiate);</p></blockquote> <p align=left>}</p> <p align=left></font><font color="#0000ff" size=2>if</font><font size=2> (isAuthentic)</p> <p align=left>{</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>m_userPrincipal = </font><font color="#2b91af" size=2>UserPrincipal</font><font size=2>.FindByIdentity(m_principalContext, username);</p> <p align=left></font><font color="#2b91af" size=2>Console</font><font size=2>.WriteLine(</font><font color="#a31515" size=2>&quot;User &quot;</font><font size=2> + m_userPrincipal.Name + </font><font color="#a31515" size=2>&quot; gave a valid password.\n&quot;</font><font size=2>);</p> <p align=left></font><font color="#2b91af" size=2>Console</font><font size=2>.WriteLine(</font><font color="#a31515" size=2>&quot;Last logged on &quot;</font><font size=2> + m_userPrincipal.LastLogon + </font><font color="#a31515" size=2>&quot;\n&quot;</font><font size=2>);</p> <p align=left></font><font color="#2b91af" size=2>Console</font><font size=2>.WriteLine(</font><font color="#a31515" size=2>&quot;Last bad password given &quot;</font><font size=2> + m_userPrincipal.LastBadPasswordAttempt + </font><font color="#a31515" size=2>&quot;\n&quot;</font><font size=2>);</p></blockquote> <p align=left>}</p> <p align=left></font><font color="#0000ff" size=2>return</font><font size=2> isAuthentic;</p></blockquote> <p align=left>}</p> <p align=left></font><font color="#0000ff" size=2>catch</font><font size=2> (</font><font color="#2b91af" size=2>Exception</font><font size=2> ex)</p> <p align=left>{</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left></font><font color="#2b91af" size=2>Console</font><font size=2>.WriteLine(ex.ToString() + </font><font color="#a31515" size=2>&quot;\n&quot;</font><font size=2>);</p> <p align=left></font><font color="#0000ff" size=2>return</font><font size=2> isAuthentic;</p></blockquote> <p align=left>}</p></blockquote> <p align=left>}</p> <p align=left></font><font color="#0000ff" size=2>static</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> Main(</font><font color="#0000ff" size=2>string</font><font size=2>[] args)</p> <p align=left>{</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left></font><font color="#0000ff" size=2>string</font><font size=2> user = </font><font color="#a31515" size=2>&quot;&quot;</font><font size=2>;</p> <p align=left></font><font color="#0000ff" size=2>string</font><font size=2> pass = </font><font color="#a31515" size=2>&quot;&quot;</font><font size=2>;</p> <p align=left></font><font color="#0000ff" size=2>bool</font><font size=2> good = </font><font color="#0000ff" size=2>false</font><font size=2>;</p> <p align=left></font><font color="#0000ff" size=2>while</font><font size=2> (</font><font color="#0000ff" size=2>true</font><font size=2>)</font></p></blockquote> <blockquote dir=ltr style="margin-right:0px"> <p align=left><font size=2>{</p></blockquote> <blockquote dir=ltr style="margin-right:0px"> <blockquote dir=ltr style="margin-right:0px"> <p align=left></font><font color="#2b91af" size=2>Console</font><font size=2>.WriteLine(</font><font color="#a31515" size=2>&quot;Please enter a username:&quot;</font><font size=2>);</p> <p align=left>user = </font><font color="#2b91af" size=2>Console</font><font size=2>.ReadLine();</p> <p align=left></font><font color="#2b91af" size=2>Console</font><font size=2>.WriteLine(</font><font color="#a31515" size=2>&quot;Please enter &quot;</font><font size=2> + user + </font><font color="#a31515" size=2>&quot;'s password:&quot;</font><font size=2>);</p> <p align=left>pass = </font><font color="#2b91af" size=2>Console</font><font size=2>.ReadLine();</p> <p align=left>good = </font><font color="#2b91af" size=2>Program</font><font size=2>.AuthenticateUser(user, pass);</font></p> <p align=left><font color="#2b91af" size=2>Console</font><font size=2>.WriteLine(</font><font color="#a31515" size=2>&quot;The result was &quot;</font><font size=2> + good.ToString() + </font><font color="#a31515" size=2>&quot;.\nHit enter to continue.&quot;</font><font size=2>);</p> <p align=left></font><font color="#2b91af" size=2>Console</font><font size=2>.ReadLine(); </p></blockquote> <p align=left>}</p></blockquote> <p align=left>}</p></blockquote> <p align=left>}</p> <p align=left>}</p></font> <p align=left> </p></div></div> <p align=left> </p></font> <p></p><font size=2> <p align=left></p></font>Tue, 22 Jan 2008 06:38:51 Z2009-11-26T06:11:21Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/adf1b027-d8a0-4bbd-9b3e-8f3bb6d58879http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/adf1b027-d8a0-4bbd-9b3e-8f3bb6d58879MrRichDeanhttp://social.msdn.microsoft.com/Profile/en-US/?user=MrRichDeanDistinct QueryHello all.<br/><br/>I have the following LINQ statement that appears to be working fine and dandy. Hwoever, someone has pointed out that my distinct may not be doing exactly what I want it to do (i.e. essentially group the UnitUserfield1). <br/><br/>Can someone explain the logic behind this and point me in the right direction as to how to amend this? The code is as follows:<br/><br/> <pre> var query = (from r in db.tblOnlineReportingFINALPRODUCTs where r.MemberId.Equals(&quot;FM00012&quot;) select new { unitUserfield1 = r.UnitUserfield1 }).Distinct().OrderBy(r =&gt; r.unitUserfield1);</pre>Tue, 24 Nov 2009 09:39:01 Z2009-11-25T15:45:02Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/85af14af-2686-4ff2-8948-d22118eb93cdhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/85af14af-2686-4ff2-8948-d22118eb93cdCarmineChttp://social.msdn.microsoft.com/Profile/en-US/?user=CarmineCDoes LINQ support the HDF5 file format?Does LINQ support the HDF5 file format?<br/><br/>Thanks,<br/><br/> <p class=MsoNormal style="margin:0in 0in 0pt"><a name="_MailAutoSig"><strong><span style=""><span style="font-size:small"><span style="font-family:Calibri">Carmine Coniglio</span></span></span></strong></a></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style=""><em><span style="font-size:10pt"><span style="font-family:Calibri">Account Technology Strategist</span></span></em></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style=""><em><span style="font-size:10pt"><span style="font-family:Calibri">Microsoft Corporation</span></span></em></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style=""><em><span style="font-size:10pt"><span style="font-family:Calibri">1290 Avenue of the Americas</span></span></em></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style=""><em><span style="font-size:10pt"><span style="font-family:Calibri">New York, NY 10104</span></span></em></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style=""><em><span style="font-size:10pt"><span style="font-family:Calibri">Cell: 646 225-5066</span></span></em></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><a href="mailto:carmine@microsoft.com"><span style=""><em><span style="color:blue;font-size:10pt"><span style="font-family:Calibri">carminec@microsoft.com</span></span></em></span></a><span style=""><em><span style="font-size:10pt"></span></em></span></p>Thu, 19 Nov 2009 18:46:05 Z2009-11-27T03:39:59Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/61fdde72-9212-4fbb-af69-d225586da1edhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/61fdde72-9212-4fbb-af69-d225586da1edCTRLIThttp://social.msdn.microsoft.com/Profile/en-US/?user=CTRLITXElement.Add Problem<p>Please tell me what I am doing wrong, but the syntax seems correct, however, no matter how I try to perform adding some elements to a larger group of elements, it cannot be done.<br/><br/>I also tried creating the small single elements and then adding to the larger group, but this wasnt allowed either. Perhaps there is a better method of doing this easy task.</p> <pre lang=x-vbnet> Dim status As XElement = _ New XElement(&quot;status&quot;, _ New XAttribute(&quot;state&quot;, &quot;full&quot;), _ New XElement(&quot;pc&quot;, _ New XAttribute(&quot;mac&quot;, &quot;0000000&quot;), _ New XAttribute(&quot;serial&quot;, &quot;123321&quot;) _ ), _ New XElement(&quot;test1&quot;, _ New XAttribute(&quot;state&quot;, &quot;off&quot;), _ New XAttribute(&quot;vol&quot;, &quot;0&quot;) _ ), _ New XElement(&quot;test2&quot;, _ New XAttribute(&quot;state&quot;, &quot;off&quot;), _ New XAttribute(&quot;vol&quot;, &quot;0&quot;) _ ) _ ) ' TEST 1 ' This doesnt work Dim x As XElement = New XElement(&quot;ext&quot;, _ New XAttribute(&quot;id&quot;, id), _ New XAttribute(&quot;cd&quot;, &quot;&quot;) _ ) status.Element(&quot;status&quot;).Add(x) ' TEST 2 ' This doesnt work spaStatus.Element(&quot;status&quot;).Add(New XElement(&quot;ext&quot;, _ New XAttribute(&quot;id&quot;, id), _ New XAttribute(&quot;cd&quot;, &quot;&quot;) _ )</pre>Wed, 18 Nov 2009 08:10:45 Z2009-11-24T19:40:01Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/222bf3c8-8bce-4609-b79f-b5eca70f166ehttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/222bf3c8-8bce-4609-b79f-b5eca70f166eCTRLIThttp://social.msdn.microsoft.com/Profile/en-US/?user=CTRLITSaving XDocument in My.Settings/user.configI am trying to save an XDocument into the application user settings. When I do this, nothing happens.<br/><br/>First I create the XDocument.<br/><br/>Dim x As XDocument = New XDocument(&lt;test /&gt;)<br/>My.Settings.x = New XDocument(x)<br/>My.Settings.Save<br/><br/>After I review the user.config file the following is generated:<br/><br/>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br/>&lt;configuration&gt;<br/>    &lt;userSettings&gt;<br/>        &lt;MyApp.Settings&gt;<br/>            &lt;setting name=&quot;x&quot; serializeAs=&quot;Xml&quot;&gt;<br/>                &lt;value /&gt;<br/>            &lt;/setting&gt;<br/>        &lt;/MyApp.Settings&gt;<br/>    &lt;/userSettings&gt;<br/>&lt;/configuration&gt;<br/><br/>So I know the setting is being accessed or else the setting x and the &lt;value /&gt; elements wouldnt exist. There is no exception thrown. So either you cannot store XML data w/in the XML Configuration file, but thought this would be serialized for storage, but maybe not, I have never attempted this. I can simply write to a file, but am not a fan of this approach, this is much cleaner and doesnt require adding more files to our application.<br/><br/>Thanks,<br/><br/>SteveThu, 19 Nov 2009 03:06:30 Z2009-11-24T19:39:27Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/4b82f3c2-0e0e-4297-8692-10ab30cbbc1dhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/4b82f3c2-0e0e-4297-8692-10ab30cbbc1dsagngh8http://social.msdn.microsoft.com/Profile/en-US/?user=sagngh8Linq to sql translationHi i need a linq to sql query for this... <div><br/></div> <div>select LanguageName from Language where languageID not in(select LanguageId from Translation where SourceKey=&quot;SourceKey&quot;)</div> <div><br/></div> <div>Please note that the innerquery returns me the (multiple) language id (bcoz source key is in multiple languages)</div> <div><br/></div> <div>Thanks</div>Tue, 24 Nov 2009 12:26:20 Z2009-11-24T18:37:17Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/5a1d7e1b-9d83-49fe-8972-55eb66918fd7http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/5a1d7e1b-9d83-49fe-8972-55eb66918fd7suvorovhttp://social.msdn.microsoft.com/Profile/en-US/?user=suvorovexpression cannot contain query expressionsHi All,<br/> By any chance anybody can help with this issue:<br/> <br/> 1. There is entity framework context.<br/> public partial class DataModelContainer : ObjectContext<br/> ..... blah<br/> <br/> 2. Other class use this context. HomeAddress is entity framework class. HomeAddressDTO use for projections from HomeAddress.<br/> <br/> using( var context = new DataModelContainer() )<br/>  {<br/> var addressRange = ( context.AddressSet.OfType&lt;HomeAddress&gt;().OrderBy( addr =&gt; addr.Id ).AsEnumerable&lt;HomeAddress&gt;().Skip&lt;HomeAddress&gt;( skip ).Take&lt;HomeAddress&gt;( take ) ).ToList&lt;HomeAddress&gt;();<br/> <br/> var progectedRange = from address in addressRange select new HomeAddressDTO { Id = address.Id, City = address.City, Zip = address.Zip, PartyId = address.Party.Id };<br/> <br/> return progectedRange.AsEnumerable&lt;HomeAddressDTO&gt;();<br/> <br/> }<br/> <br/> 3. What I meant to do, take range from home address set and project to DTO.<br/> But I get this &quot;expression cannot contain query expressions&quot; error. What is went wrong ?<br/> <br/> Thanks,<br/> Alex<br/>Wed, 18 Nov 2009 22:17:59 Z2009-11-25T03:33:44Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/f6410ef6-8f46-41e8-9355-ac794823c1dahttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/f6410ef6-8f46-41e8-9355-ac794823c1dasathiavelhttp://social.msdn.microsoft.com/Profile/en-US/?user=sathiavelRemove duplicate rows based on max(date)<p>Hi Greetings!<br/><br/>I need a query to retain the records with latest date and deleted the duplicates with older dates<br/>eg.<br/><span class=str><span style="color:#800000">''''''''''''''''''''''''''''''''''''''''''''''''''''<br/>id     date         row_id(unique)<br/>1      11/11/2009    1<br/>1      12/11/2009    2<br/>1      13/11/2009    3<br/>2      1/11/2009     4<br/>'''''''''''''''''''''''''''''''''''''''''''''''''''<br/></span>Expected result</span></p> <pre class=prettyprint><code><span class=lit><span style="color:#800000">1</span></span><span class=pln>      </span><span class=lit><span style="color:#800000">13</span></span><span class=pun>/</span><span class=lit><span style="color:#800000">11</span></span><span class=pun>/</span><span class=lit><span style="color:#800000">2009</span></span><span class=pln>    </span><span class=lit><span style="color:#800000">3</span></span><span class=pln><br/></span><span class=lit><span style="color:#800000">2</span></span><span class=pln>      </span><span class=lit><span style="color:#800000">1</span></span><span class=pun>/</span><span class=lit><span style="color:#800000">11</span></span><span class=pun>/</span><span class=lit><span style="color:#800000">2009</span></span><span class=pln>     </span><span class=lit><span style="color:#800000">4</span></span><span class=pln><br/></span></code><span style="color:#800000">''''''''''''''''''''''''''</span><br/><br/><br/><br/><br/><br/><br/><br/></pre> Please help me<br/>SathiaMon, 23 Nov 2009 13:17:48 Z2009-11-24T08:09:58Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/762082a5-868b-4ab8-9cb5-c1a9e45772d8http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/762082a5-868b-4ab8-9cb5-c1a9e45772d8Frank_LAhttp://social.msdn.microsoft.com/Profile/en-US/?user=Frank_LALINQ orderby not workingwhen I use LINQ orderby , for example col1<br/> when sql server 2008 has an index on col1 and it sort by descending<br/> LINQ return the result in descending order<br/> <br/> Is it a bug or it is the way LINQ works?<br/> <br/> Thanks very much<br/> <br/>Mon, 23 Nov 2009 03:40:41 Z2009-11-24T07:07:50Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/2b14338e-ac67-40ed-a63f-62c450f9e490http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/2b14338e-ac67-40ed-a63f-62c450f9e490Waleed Seadahttp://social.msdn.microsoft.com/Profile/en-US/?user=Waleed%20SeadaConvert datagridview to complex XML<p>Dear all,<br/><br/>I have populated a datgridview with data.<br/>I need to structure these data in a complex XML file as follows :<br/><br/>&lt;root&gt;<br/>  &lt;From&gt;External Entity&lt;/From&gt;<br/>  &lt;To&gt;Internal Interface&lt;/To&gt;<br/>  &lt;Region One&gt;<br/>    &lt;TP&gt;200107070&lt;/TP&gt;<br/>    &lt;Data&gt;<br/>      &lt;Address&gt;&lt;/Address&gt;<br/>      &lt;Stores&gt;<br/>        &lt;Location&gt;any location&lt;/Location&gt;<br/>        &lt;Element&gt;<br/>          &lt;Name&gt;Prod-1&lt;/Name&gt;<br/>          &lt;Value&gt;20914221.00&lt;/Value&gt;<br/>        &lt;/Element&gt;<br/>        &lt;Element&gt;<br/>          &lt;Name&gt;Prod-2&lt;/Name&gt;<br/>          &lt;Value&gt;5000.00&lt;/Value&gt;<br/>        &lt;/Element&gt;<br/>      &lt;/Stores&gt;<br/>    &lt;/Data&gt;<br/>  &lt;/Region One&gt;<br/>&lt;/root&gt;<br/><br/>How can this be done using LINQ..<br/><br/>Best regards</p><hr class="sig">W.SeadaThu, 12 Nov 2009 12:09:30 Z2009-11-24T03:00:13Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/4c06cda9-5eb7-43e3-be8b-c59c27b02568http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/4c06cda9-5eb7-43e3-be8b-c59c27b02568Greg05http://social.msdn.microsoft.com/Profile/en-US/?user=Greg05Convert SQL to LINQI'm trying to convert the following SQL to LINQ, but so far I'm not getting the right results.  Anyone know the best way to right this?<br/> <br/> <pre>select p.* from photo p join (select pt.photoid from phototag pt join tag t on t.tagid = pt.tagid where t.description = 'keyword' or t.description = 'keyword2' group by pt.photoid having count(1) = 2) as pt on pt.photoid = p.photoid </pre> <br/> Basically I have three tables.  One contains photo information (photo), another that contains tags that describe the photos (tag), and the last one to specify which tags go with which photos (phototag).  The search can be one or more keywords, but in the example above it's only 2.  Search is always AND.Tue, 24 Nov 2009 00:18:05 Z2009-11-24T02:19:32Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/caeec8a5-19c2-4f48-a67c-b05e85898d86http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/caeec8a5-19c2-4f48-a67c-b05e85898d86Xvengerhttp://social.msdn.microsoft.com/Profile/en-US/?user=XvengerLinqDataSource doesn't handle original NULL values up updates for int, datetime, etc<p align=left><font face=Arial size=2></font> </p> <p>Has anyone else encoutered this problem:</p> <p> </p> <p>I am using a LinqDataSource, and I have several rows that have null values for ints, datetimes, etc.  When I try to do an update to that object from a formview, (even though I have now provided a value), LinqDataSource throws an exception from trying to cast the original NULL value to the type it is, which of course doesn't work for ints, datetimes, etc.  This is pretty annoying, considering that I have to do stupid things to work around this such as using some generic date as a default value.  I am assuming this is a bug.  Has anyone else noticed this?  Are there any work around or fixes?</p> <p> </p> <p>Thanks,</p> <p> </p> <p>John Sonmez</p>Fri, 12 Oct 2007 15:01:25 Z2009-11-23T20:53:33Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/998dbae8-17ea-4026-8fcb-acafbb5dc37ahttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/998dbae8-17ea-4026-8fcb-acafbb5dc37aMadhanGaneshhttp://social.msdn.microsoft.com/Profile/en-US/?user=MadhanGaneshHow to Sum the grouped fieldI've a list of tasks. Ie I've struct like<br/>     struct Task<br/>     {<br/>         public DateTime Completed { get; set; }<br/>         public string TaskKind { get; set; }<br/>         public float Effort { get; set; }<br/>     }<br/> <br/> And I've a IList&lt;Task&gt;.<br/> <br/> Now I want to group the tasks based on the date and group by TaskKind on each day with the summed effort.<br/> For eg for I want to print like the below:<br/>   <br/> 01-09-09     Design:10     Coding:5    Coordination:15<br/> 02-09-09     Coding:5       Meeting:2<br/> <br/> Can anyone help me in constructing a LINQ query to achieve this?<br/> <br/> Thanks.<br/> MadhanSun, 22 Nov 2009 14:04:37 Z2009-11-23T14:30:40Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/66a0eee0-cccd-44ce-9917-6a388d923a6ahttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/66a0eee0-cccd-44ce-9917-6a388d923a6ash856531http://social.msdn.microsoft.com/Profile/en-US/?user=sh856531How do I do an IF EXISTS style query using LINQ?<p align=left>Hi all,</p> <p align=left> </p> <p align=left>Quite often in SQL I need to be able to do a very quick query to determine if a given record exists meaning that I do something like:-</p> <p align=left> </p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left>IF EXISTS (SELECT 1 FROM tblUsers WHERE Username = &quot;whatever&quot;)</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>RETURN 1</p></blockquote> <p align=left>ELSE</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>RETURN 0</p></blockquote> <p align=left> </p></div></div> <p align=left> </p> <p>In SQL this is extremely fast and returns me a bit that tells me very simply if the record exists or not.</p> <p align=left> </p> <p align=left>Could anyone tell me how I would write the same sort of thing in LINQ without having to return whole objects. At the moment all the samples seem to return the entire &quot;User&quot; object whereas I only want a bool value returned from the linq query.</p> <p align=left> </p> <p align=left>Many thanks to anyone who can help</p> <p align=left> </p> <p align=left>:-)</p> <p align=left> </p> <p align=left>Simon</p> <p align=left> </p> <p align=left> </p>Tue, 12 Aug 2008 10:56:57 Z2009-11-23T02:52:11Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/46a77abc-338c-4621-a20e-7892ff2f04c7http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/46a77abc-338c-4621-a20e-7892ff2f04c7pkellnerhttp://social.msdn.microsoft.com/Profile/en-US/?user=pkellnerAny way to get multiple SUM()'s executed AND how to protect against no rows being returnedI&quot;m taking a chance and asking two questions here (not first normal form of forum question asking).<br/><br/>1.  I've got the below sum's which causes 4 executions against the database.  Is there anyway to have these sum's done in 1 query pass?<br/><br/>2.  When I get 0 rows returns, the Sum() fails.  What is a simple work around for this?  I can't figure out what delegate possibly to pass to sum().<br/><br/><span style="font-size:xx-small"> <p> </p> <pre>query.TotalHistoricalSpend = resultsQuery.Select(data =&gt; data.HistoricalSpend ?? 0.0).Sum(); query.TotalOptimalSpend = resultsQuery.Select(data =&gt; dataOptimalSpend ?? 0.0).Sum(); query.TotalSpend = resultsQuery.Select(data =&gt; data.Spend ?? 0.0).Sum(); query.TotalWeight = resultsQuery.Select(data =&gt; data.ShipmentWeight ?? 0.0).Sum();</pre> </span><hr class="sig">Peter Kellner, MVP ASP.NET http://peterkellner.netSat, 14 Nov 2009 17:15:23 Z2009-11-23T00:49:51Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/90331f09-7262-4238-9238-99d5e31fc25chttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/90331f09-7262-4238-9238-99d5e31fc25cBill_Yeagerhttp://social.msdn.microsoft.com/Profile/en-US/?user=Bill_YeagerRemove method in LINQ to OBjectsAll I want to do is to <strong>remove</strong> an item from a generic collection using the <strong>Remove</strong> method. I can't find any clear docs on how to do that.<br/><br/>Could somebody please let me know how I can go about that?<br/><br/>Thanks,<br/><br/>Bill<hr class="sig">Bill YeagerSat, 21 Nov 2009 03:02:43 Z2009-11-22T22:49:09Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/64c2076c-236e-4975-a66b-6fe465b4925bhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/64c2076c-236e-4975-a66b-6fe465b4925babtcabehttp://social.msdn.microsoft.com/Profile/en-US/?user=abtcabeWhere / (i And 1) = 1)<p class=MsoNormal style="margin:0in 0in 0pt"><span style=""><span style="font-size:small"><span style="font-family:Times New Roman">I am currently reading ProLINQ in VB2008.<span style="">  </span>This book has the following code example:</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style=""><span style="font-family:Times New Roman;font-size:small"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">    </span>Console.WriteLine(<span style="color:#a31515">&quot;{0} : Begin&quot;</span>, <span style="color:blue">New</span> StackTrace(0, <span style="color:blue">True</span>).GetFrame(0).GetMethod().Name)</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">    </span><span style="color:blue">Dim</span> presidents() <span style="color:blue">As</span> <span style="color:blue">String</span> = { _</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">      </span><span style="color:#a31515">&quot;Adams&quot;</span>, <span style="color:#a31515">&quot;Arthur&quot;</span>, <span style="color:#a31515">&quot;Buchanan&quot;</span>, <span style="color:#a31515">&quot;Bush&quot;</span>, <span style="color:#a31515">&quot;Carter&quot;</span>, <span style="color:#a31515">&quot;Cleveland&quot;</span>, _</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">      </span><span style="color:#a31515">&quot;Clinton&quot;</span>, <span style="color:#a31515">&quot;Coolidge&quot;</span>, <span style="color:#a31515">&quot;Eisenhower&quot;</span>, <span style="color:#a31515">&quot;Fillmore&quot;</span>, <span style="color:#a31515">&quot;Ford&quot;</span>, <span style="color:#a31515">&quot;Garfield&quot;</span>, _</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">      </span><span style="color:#a31515">&quot;Grant&quot;</span>, <span style="color:#a31515">&quot;Harding&quot;</span>, <span style="color:#a31515">&quot;Harrison&quot;</span>, <span style="color:#a31515">&quot;Hayes&quot;</span>, <span style="color:#a31515">&quot;Hoover&quot;</span>, <span style="color:#a31515">&quot;Jackson&quot;</span>, _</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">      </span><span style="color:#a31515">&quot;Jefferson&quot;</span>, <span style="color:#a31515">&quot;Johnson&quot;</span>, <span style="color:#a31515">&quot;Kennedy&quot;</span>, <span style="color:#a31515">&quot;Lincoln&quot;</span>, <span style="color:#a31515">&quot;Madison&quot;</span>, <span style="color:#a31515">&quot;McKinley&quot;</span>, _</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">      </span><span style="color:#a31515">&quot;Monroe&quot;</span>, <span style="color:#a31515">&quot;Nixon&quot;</span>, <span style="color:#a31515">&quot;Pierce&quot;</span>, <span style="color:#a31515">&quot;Polk&quot;</span>, <span style="color:#a31515">&quot;Reagan&quot;</span>, <span style="color:#a31515">&quot;Roosevelt&quot;</span>, <span style="color:#a31515">&quot;Taft&quot;</span>, _</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">      </span><span style="color:#a31515">&quot;Taylor&quot;</span>, <span style="color:#a31515">&quot;Truman&quot;</span>, <span style="color:#a31515">&quot;Tyler&quot;</span>, <span style="color:#a31515">&quot;Van Buren&quot;</span>, <span style="color:#a31515">&quot;Washington&quot;</span>, <span style="color:#a31515">&quot;Wilson&quot;</span>}</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">    </span><span style="color:blue">Dim</span> sequence <span style="color:blue">As</span> IEnumerable(<span style="color:blue">Of</span> <span style="color:blue">String</span>) = _</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">      </span>presidents.Where(<span style="color:blue">Function</span>(p, i) (i <span style="color:blue">And</span> 1) = 1)</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">    </span><span style="color:blue">For</span> <span style="color:blue">Each</span> s <span style="color:blue">As</span> <span style="color:blue">String</span> <span style="color:blue">In</span> sequence</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">      </span>Console.WriteLine(<span style="color:#a31515">&quot;{0}&quot;</span>, s)</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">    </span><span style="color:blue">Next</span> s</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><span style="">    </span>Console.WriteLine(<span style="color:#a31515">&quot;{0} : End&quot;</span>, <span style="color:blue">New</span> StackTrace(0, <span style="color:blue">True</span>).GetFrame(0).GetMethod().Name)</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt">The results of this code is every other item in the array starting with Arhtur (Arhur, Bush, Cleveland, etc.)</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt">What is the following command doing: (i <span style="color:blue">And</span> 1)</span><span style=""></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt">I understand i is the index of the given item in the array, however I do not understand when or how (i And 1) = 1. </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-family:'Courier New';font-size:10pt"><br/>Thanks for your help. </span></p> <p> </p>Sat, 21 Nov 2009 23:37:23 Z2009-11-22T02:15:44Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/ee4b3413-a0a2-4888-b805-2b6ecd370e66http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/ee4b3413-a0a2-4888-b805-2b6ecd370e66Janus007http://social.msdn.microsoft.com/Profile/en-US/?user=Janus007Extension method XElementNeed a little hint here...<br/> Normally I can create extension methods, but this one I'm having some trouble with:<br/> <br/> <pre lang="x-c#">var q = xml.Descendants(ns + &quot;Event&quot;).ToList(); //q returns 1, everything is fine :) var q1 = xml.Descendants(ns + &quot;Events&quot;).Foo().ToList(); //q1 returns 0, something is wrong :( public static class XMLExtension { public static IEnumerable&lt;XElement&gt; Foo&lt;XElement&gt;(this IEnumerable&lt;XElement&gt; value) { return value; } }</pre> <br/> What is wrong?<br/> <br/>Thu, 19 Nov 2009 20:59:21 Z2009-11-21T11:13:48Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/c139313e-d745-4e1d-b3dc-ab355507eb48http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/c139313e-d745-4e1d-b3dc-ab355507eb48Joe Albaharihttp://social.msdn.microsoft.com/Profile/en-US/?user=Joe%20AlbahariRecommended way to do outer joins in LINQ to SQLFirst option:<br><br>    var query =<br>        from c in Data.Customers<br>        from p in c.Purchases.DefaultIfEmpty()<br>        select new<br>        {<br>            c.Name,<br>            p.Price<br>        };<br><br>This produces the SQL you'd expect, i.e.<br><br>    SELECT [t0].[Name], [t1].[Price]<br>    FROM [Customer] AS [t0]<br>    LEFT OUTER JOIN [Purchase] AS [t1] ON [t1].[CustomerID] = [t0].[ID]<br><br>Second option:<br><br>    var query =<br>        from c in Data.Customers<br>        from p in c.Purchases.DefaultIfEmpty()<br>        select new<br>        {<br>            c.Name,<br>            <span style="font-weight:bold">Price = (p == null ? null : p.Price)</span><br>        };<br><br>This type of query works over both IEnumerable and IQueryable, but the SQL it produces seems less efficient:<br><br>SELECT [t3].[Name], [t3].[value] AS [Price]<br>FROM (<br>    SELECT <br>        (CASE <br>            WHEN [t2].[test] IS NULL THEN NULL<br>             ELSE [t2].[Price]<br>         END) AS [value], [t0].[Name]<br>    FROM [Customer] AS [t0]<br>    LEFT OUTER JOIN (<br>        SELECT 1 AS [test], [t1].[CustomerID], [t1].[Price]<br>        FROM [Purchase] AS [t1]<br>        ) AS [t2] ON [t2].[CustomerID] = [t0].[ID]<br>    ) AS [t3]<br><br>Which is the recommended way to do an outer join? Should the query be written differently depending on whether it's going to run over LINQ to SQL or a local collection?<br><br>Joe<br><br>Sat, 21 Oct 2006 01:50:47 Z2009-11-20T23:23:07Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/ce0e4d12-e586-4f01-9b3e-14087cb04006http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/ce0e4d12-e586-4f01-9b3e-14087cb04006Dr Griffhttp://social.msdn.microsoft.com/Profile/en-US/?user=Dr%20GriffLinq to Excel - how?<p align=left><font face=Arial size=2></font>Hi</p> <p align=left> </p> <p align=left>I will be receiving XML workbooks with various worksheets in them.  I need to read data out of the various worksheets and generate bespoke formatted data from this.  I will know the name of the work sheets and the column headers.  I'd like to use LINQ to achieve this.</p> <p align=left> </p> <p align=left>In my past life as a VB6 developer, I would have written an ADO query and retrieved a recordset and then worked with that.  As a LINQ novice, my uninformed instinct would be to Save the excel spread sheet as an XML document (would probably get rid of the Excel version problem) and then query the XML using LINQ.  But this may not be the best way....</p> <p align=left> </p> <p align=left>My question therefore is &quot;how best should I go about this?&quot;</p> <p align=left> </p> <p align=left>Many thanks </p> <p align=left> </p> <p align=left>Griff</p> <p align=left> </p> <p align=left>PS - I did attempt to save it as XML and query it using LINQ, but I think I fell foul of the namespaces.  A quick look on MSDN baffled me further.....are there any good tutorials out there?</p>Tue, 06 May 2008 16:19:54 Z2009-11-20T17:39:04Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/b1480b00-44ec-4b32-984f-cdd53d87e99dhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/b1480b00-44ec-4b32-984f-cdd53d87e99dBBeattiehttp://social.msdn.microsoft.com/Profile/en-US/?user=BBeattieVariables in LINQ Lambda Expressions cause expression to re-evaluate dynamically if changed.I found an issue with LINQ dynamically re-evaluating the results of an expression when a variable used in the expression is changed after the expression is evaluated. <div><br/></div> <div>I put sample code on my blog.</div> <div><br/></div> <div><a href="http://babeattie.blogspot.com/2009/11/beware-reference-variables-in-linq.html">http://babeattie.blogspot.com/2009/11/beware-reference-variables-in-linq.html</a></div> <div><br/></div> <div>Is this intended behaviour, or a quirky bug? It is certainly something to be wary of when using lambda expressions in recursion.</div> <div><br/></div> <div><br/></div> <div>SAMPLE CODE - changing the indx value after the count is evaluated changes the result set immediately!</div> <div><br/></div> <div><br/></div> <div><span style="font-family:'Trebuchet MS', Verdana, Arial, sans-serif;font-size:13px;color:#3333ff;line-height:18px"><span style="font-family:'courier new'"><span style="font-size:x-small">private void button1_Click(object sender, EventArgs e)<br/>{<br/>System.Diagnostics.Debug.WriteLine(&quot;Testing:&quot; + DateTime.Now.ToString());<br/>List&lt;Dictionary&lt;int, int&gt;&gt; ld = new List&lt;Dictionary&lt;int, int&gt;&gt;();<br/><br/>ld.Add(new Dictionary&lt;int, int&gt;() { { 1, 10 }, { 2, 20 }, { 3, 30 } });<br/>ld.Add(new Dictionary&lt;int, int&gt;() { { 1, 20 }, { 2, 300000}, { 3, 40000000 }, { 4, 500000000 }});<br/><br/>int indx = 10;<br/><br/>var ds = ld.Where(itm =&gt; itm[1] == indx);<br/><br/>if (ds.Count() &gt; 0)<br/>{<br/>System.Diagnostics.Debug.WriteLine(&quot;Count=&quot; + ds.First().Count());<br/>indx = ds.First()[2]; //this is a value I want to keep for later<br/>int valueIwant = ds.First()[3]; //this is the value I want from this result<br/><br/>System.Diagnostics.Debug.WriteLine(&quot;Value I Want=&quot; + valueIwant);<br/>//oh no ??? that's not the value I wanted ???<br/>System.Diagnostics.Debug.WriteLine(&quot;Count=&quot; + ds.First().Count());<br/>//and now the count changed ???<br/>}<br/>}</span></span><span style="font-size:x-small"> </span></span></div>Tue, 10 Nov 2009 03:31:10 Z2009-11-20T11:15:04Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/d6ef58af-53e1-4fd3-b240-cdf6db9cc8f0http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/d6ef58af-53e1-4fd3-b240-cdf6db9cc8f0Wonko_TheSanehttp://social.msdn.microsoft.com/Profile/en-US/?user=Wonko_TheSaneQuerying for a sublistPerhaps its just because its been a long couple of days, but I can't seem to figure this out...  :)<br/> <br/> Let's say I have the following (partial) classes, judging some kind of contest (forgive the hokey example, again, it's been a long day):<br/> <br/> <pre lang="x-c#">class SingleEvent<br/> {<br/> public Double Score<br/> {<br/> get;<br/> set;<br/> } <br/> }<br/> <br/> class Contestant<br/> {<br/> public List&lt;SingleEvent&gt; AllScores<br/> {<br/> get;<br/> set;<br/> }<br/> <br/> public int ID<br/> {<br/> get;<br/> set;<br/> }<br/> }<br/> </pre> So, as you can see, a contestant gets a score for each individual event, and a list of those scores is kept in another class.<br/> <br/> Now, for the entire contest, I have this class:<br/> <pre lang="x-c#">class AllContestants { public List&lt;Contestant&gt; Contestants { get; set; } }</pre> <br/> I now want to create a query against AllContestants that will bring me back all the scores in descending score order (not grouped by events).<br/> <br/> Make any sense at all?<br/> <br/> Thanks,<br/> WtSWed, 18 Nov 2009 22:19:05 Z2009-11-26T00:40:12Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/571c86a8-3a72-41d7-a066-9d20c7905307http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/571c86a8-3a72-41d7-a066-9d20c7905307Massie92http://social.msdn.microsoft.com/Profile/en-US/?user=Massie92more than 1 table in a gridviewHello<br/> <br/> i search on the internet how i can get data from more than 1 table in a gridview, but i need it for a windows form. I don't understand one thing of asp.net. So can some of you give me a solution. And if it's possible that i can add columns so not hole the table but only column one, two and three for example.Sat, 07 Nov 2009 09:42:19 Z2009-11-20T10:02:37Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/61981f3b-8ac3-405c-b99f-35a4ead9806bhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/61981f3b-8ac3-405c-b99f-35a4ead9806bEgilOfBorghttp://social.msdn.microsoft.com/Profile/en-US/?user=EgilOfBorgPossible LINQ/SQL Server memory leak<p align=left>Hi</p> <p align=left> </p> <p align=left>I'm using LINQ to SQL from VS 2008 with SQL Server 2005 (dev edition - sp2) and developing a web application, doing my testing with VS internal web server.</p> <p align=left> </p> <p align=left>After 6-8 hours of developing, hitting the database quite alot, it suddenly becomes very unresponsive. I can fix it by restarting the SQL service, which leads me to believe there are unclosed/dead connection or LINQ to SQL is causeing problems.</p> <p align=left> </p> <p align=left>The queries I perform are pretty basic (select from one or more tables with some custom projection and basic filtering), so I'm pretty sure thats not the source of the problem.</p> <p align=left> </p> <p align=left>I try to limit the number of datacontexts I create, so for each request I only use one datacontext, like so:</p> <p align=left> </p><font color="#2b91af"> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Block</span></div><font color="#2b91af"> <p align=left>BTDataContext</font> db = (<font color="#2b91af">HttpContext</font>.Current.Items[<font color="#a31515">&quot;DB&quot;</font>] ?? (<font color="#2b91af">HttpContext</font>.Current.Items[<font color="#a31515">&quot;DB&quot;</font>] = <font color="#0000ff">new</font> <font color="#2b91af">BTDataContext</font>())) <font color="#0000ff">as</font> <font color="#2b91af">BTDataContext</font>;</p> <p align=left> </p></div></div> <p align=left> </p></font> <p></p> <p>I unsure if that could be the source of the problem, but I dont think it is.</p> <p align=left> </p> <p align=left>Any input will be much appreciated.</p> <p align=left> </p> <p align=left>Best regards, Egil.</p>Sat, 27 Oct 2007 19:02:52 Z2009-11-19T22:29:42Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/06e0c522-f173-4a90-885c-2b0233cfd664http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/06e0c522-f173-4a90-885c-2b0233cfd664Juro Koskinenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Juro%20KoskinenCustom properties in LINQ to SQL classes<p>My Order table contains an OrderTypeID (int) field in the database. The OrderTypeIDs correspond to defined C# enum types (ie. OrderType.Internal). Using a partial class I can extend the LINQ to SQL Designer generated Order class to contain a OrderType property which returns a OrderType value instead of an int value. If I use the property in a query</p> <p> </p><font color="#0000ff" size=1> <p><font size=3>var</font></font> orderList =</p> <p><font color="#0000ff">from</font> order <font color="#0000ff">in</font> CurrentDataContext.Orders</p> <p><font color="#0000ff">where</font> order.OrderType == OrderType.Internal</p> <p><font color="#0000ff">select</font> order;</p> <p> </p> <p>I get an exeption Binding error : Member '...Order.OrderType' is not a mapped member of '...Order'. The OrderType property works fine on a order object, but when querying with OrderType an exception is thrown.</p>Mon, 28 May 2007 11:52:50 Z2009-11-19T22:23:16Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/460c4b1d-061d-4f22-baeb-0385f9c79546http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/460c4b1d-061d-4f22-baeb-0385f9c79546newbie26http://social.msdn.microsoft.com/Profile/en-US/?user=newbie26Help Hi i need some kind of little help here, there are no errors but it has something that i don't like<br/> when i trigger the btnReturnBook it will save to database the dateTime.Now<br/> but when i trigger it again the column of my database indicates a null it only works on the first record <br/> thanks.<br/> <br/>  private void btnReturnBook_Click(object sender, EventArgs e)<br/>         {<br/>             LSMDataContext ret = new LSMDataContext();<br/>             HistoryTable newHisto = ret.HistoryTables.FirstOrDefault(n =&gt; n.BorrowedBook == listBox1.SelectedItem.ToString());<br/>             newHisto.DateReturned = DateTime.Now.ToString();<br/>             ret.SubmitChanges();<br/> }<br/>           Thu, 19 Nov 2009 12:05:00 Z2009-11-19T12:35:01Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/92c1cb08-fc30-4de8-bb34-2863a58a3ef7http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/92c1cb08-fc30-4de8-bb34-2863a58a3ef7code_warriorhttp://social.msdn.microsoft.com/Profile/en-US/?user=code_warriorHow much SQL i should know to do LINQ?Hi Friend, <br/><br/>I've learnt SQL up to an inermediate level : ) and i;m a .NET developer... with the Introduction of LINQ, according to what i've hear, programmers now no need to learn SQL.... <br/><br/>So my qusetion is, is it possible to do all the things that we do in SQL from LINQ without knowing SQL to an adavance level? <br/><br/>thanksWed, 18 Nov 2009 04:00:39 Z2009-11-25T01:20:18Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/45053511-cc6b-40e5-babf-8d0fa1414ddfhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/45053511-cc6b-40e5-babf-8d0fa1414ddfMark Woodyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Mark%20WoodyPreserving unknown xml tags/data<p>I deal with alot of different xml documents that are very dynamic.  I end up creating some applications which allow the manipulation of the information in these xml files for use by other applications.  I really like using the XElement class to allow me to read in the information and create objects out of the xml without any real stress.  The problem is, how do I handle the new tags that get added later on.  If I create an application that reads in X Y and Z tags, and then after everything has been changed, writes out the values I know about, how do I maintain any tags I don't have prior knowledge of?<br/><br/>Case in point, I had a file defined over a year ago.  Project required more tags to be added to an xml doc.  The application that read that xml file read in the file, wrote out a new one that deleted the new tags because it didn't know enough to save them.<br/><br/>I know I could use XPath and modify the document as it stands and get around the issue that way, but I don't want to deal with XPath.  Is there a simple way to continue reading in the files as I have been, make objects out of the pieces I know about, save the pieces I don't know about where they are then put it back out to file when the changes are done?<br/><br/>Simple example.  XML sample below was defined originally as: <br/><br/><em>&lt;Sample&gt;<br/>   &lt;TagA&gt;Test&lt;/TagA&gt;<br/>&lt;/Sample&gt;<br/><br/></em>after setting up the XDocument, I would basically create an object of whatever type and read in the tags similar to:<br/>string t = (string)node.Element(&quot;TagA&quot;);<br/><br/>change it to whatever and make any other changes then write a new document out with the values from the objects.<br/><br/>Fast forward, xml spec changes and now the sample looks like:<br/><br/><em>&lt;Sample&gt;<br/>   &lt;TagA&gt;Test&lt;/TagA&gt;<br/>   &lt;TagB ID=&quot;0&quot;&gt;New Tag&lt;/TagB&gt;<br/>&lt;/Sample&gt;<br/><br/></em>After running the original app, the TagB is not read in, nor is it written out to the new file.<br/><br/>I don't care that it can't be edited without changing the application, but I need to have backwards compatibiity at least to the point that new information is not just blown away.<br/><br/>Any suggestions other than going longhand with XPath?<br/><br/>Thanks in advance.<br/><br/>Mark</p>Tue, 17 Nov 2009 04:48:48 Z2009-11-18T16:26:21Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/8f08c58f-ac81-45dd-9884-3779c462cc07http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/8f08c58f-ac81-45dd-9884-3779c462cc07lukas43http://social.msdn.microsoft.com/Profile/en-US/?user=lukas43LINQ SqlDateTime overflow<p align=left>Hi all,</p> <p> </p> <p align=left>I do somethink like:</p> <p> </p> <p align=left>MyTable _table = new MyTable;</p> <p>_table.BornDate = new DateTime(1988, 9, 4);  // yyyy , mm , dd</p> <p>_database.MyTable.InsertOnSubmit(_table);</p> <p>_database.SubmitChanges();</p> <p> </p> <p align=left>Compilation okay, but code throws an exception - <font color="#ff0000"><strong>SqlDateTime overflow, date must be between 1/1/1753 ....................</strong></font></p> <p>Debug fails on SubmitChanges();</p> <p> </p> <p>Thanks </p>Fri, 28 Mar 2008 18:08:58 Z2009-11-18T09:10:28Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/b6f7af3c-6248-4695-95ad-0dec997670behttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/b6f7af3c-6248-4695-95ad-0dec997670beAldousAhttp://social.msdn.microsoft.com/Profile/en-US/?user=AldousAOr WhereHi<br/><br/>I have a bit of code that creates a series of where clauses dependent on user selections. Everything was working fine until I was asked to rather than a criterion on a field  being &gt; x how about x=1 or x=3 or x=5 as the query to return the data.<br/><br/>When I build the query adding the conditions (say I have selected initial query from a table in sql db using a datacontext)<br/><br/> <pre lang=x-vbnet> For Each a In listOfPossibles Dim val As Integer = a query = query.Where(Function(f)f.MyIntFeild.Value = val) Next</pre> It does so by and and and. Is there a way to do this as Or?<br/><br/>I remember seing something about stings for the queries, so build the or and submit the string but I have no idea where to look for guidence on doing that?<br/><br/>Thanks for taking the time to read.<br/><br/>Anthony<br/><br/><br/>I have seen some extension written in CTue, 17 Nov 2009 10:26:46 Z2009-11-18T07:49:35Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/1b5c9147-cc97-4b92-8df4-ac04690e1faahttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/1b5c9147-cc97-4b92-8df4-ac04690e1faaderskihttp://social.msdn.microsoft.com/Profile/en-US/?user=derskiHow to select max record after group by and convert back from annonymous typeHi I have some problems with a linq group by query trying to select for the most recent revision in some audit table. <div>Here is a brief description of the data schema,</div> <div>I have a PO table, and a PO_audit table</div> <div>PO_Audit table just tracks revisions of the PO table, the minimal schema of the PO audit table is:</div> <div>{id(int), OriginalPOId(int),RevisionNumber(int)}</div> <div><br/></div> <div>Here is a sample data</div> <div>Id       OriginalPOId    Rev</div> <div>1              1                1</div> <div>2              1                2</div> <div>3              1                3</div> <div>4              2                1</div> <div>5              2                2</div> <div><br/></div> <div>In my query I'm trying to group by the original PO id and select for the most recent revision (I essentially want records with id = 3 and 5, the most recent rev of the PO)</div> <div>so something like</div> <div>var a = </div> <div>from b in PO_Audit</div> <div>group b by b.OriginalPOId into g</div> <div>select new</div> <div>{<br/>   mostRecentPO = </div> <div>(</div> <div>  from d in g</div> <div>  where d.rev == g.max(r=&gt;r.Rev)</div> <div>  select d).firstOfDefault<br/>}</div> <div>SO the above query will return a correct set of records, that is the most recent revision for each of the PO records but these are annonymous and I don't know how to convert them back to PO_Audit type other then using a for loop</div> <div><br/></div> <div>foreach(var auditPO in a)</div> <div>{</div> <div>   PO_Audit mostRecentAuditPerPO =  (PO_Audit)auditPO.mostRecentPO;</div> <div>}</div> <div><br/></div> <div>I think I'm doing something wrong in my select query because auditPO should already be of type PO_Audit, I don't think I should have to access it using auditPO.mostRecentPO. (but I could not figure out how to select for the object without using mostRecentPO = in my annonymous inner query)</div> <div><br/></div> <div>I'm looking for some way to cast these to the Ienumerable list of type PO_Audit, something like a.cast&lt;PO_Audit&gt;()</div> <div><br/></div> <div>Thank you for any help on this.</div> <div><br/></div> <div>Chris</div> <div><br/></div> <div><br/></div>Mon, 16 Nov 2009 18:26:06 Z2009-11-17T00:38:35Z