<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:37:02 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[CSHARP-502] NullReferenceException on First() call</title>
                <link>https://jira.mongodb.org/browse/CSHARP-502</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;p&gt;I am having issues with the latest MongoDB C# driver (v1.4.2.4500) crashing when I query this document:&lt;/p&gt;

&lt;p&gt;{&lt;br/&gt;
  &quot;_id&quot; : &quot;4fdfe705b48c6b24dcab994a&quot;,&lt;br/&gt;
  &quot;CreatedDate&quot; : new Date(&quot;6/18/2012 19:42:13&quot;),&lt;br/&gt;
  &quot;UpdatedDate&quot; : new Date(&quot;6/18/2012 19:42:13&quot;),&lt;br/&gt;
  &quot;SiteId&quot; : &quot;4fdfe705b48c6b24dcab9939&quot;,&lt;br/&gt;
  &quot;ClientId&quot; : &quot;4fdfe705b48c6b24dcab9937&quot;,&lt;br/&gt;
  &quot;Account&quot; : &lt;/p&gt;
{
    &quot;UserName&quot; : &quot;blah&quot;,
    &quot;Password&quot; : null,
    &quot;PasswordSalt&quot; : null,
  }
&lt;p&gt;,&lt;br/&gt;
  &quot;FbAccount&quot; : &lt;/p&gt;
{
    &quot;_id&quot; : &quot;838331911&quot;,
    &quot;access_token&quot; : &quot;.....&quot;,
    &quot;Pages&quot; : []
  }
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Query:&lt;/p&gt;

&lt;p&gt;var q1 = from u in this.Repository.All&amp;lt;User&amp;gt;()&lt;br/&gt;
         where u.FbAccount.Id == &quot;832631911&quot;&lt;br/&gt;
         select u;&lt;/p&gt;

&lt;p&gt;var foo1 = q1.First();&lt;/p&gt;

&lt;p&gt;Repository:&lt;/p&gt;

&lt;p&gt;public IQueryable&amp;lt;T&amp;gt; All&amp;lt;T&amp;gt;() where T : class, new()&lt;br/&gt;
{&lt;br/&gt;
    return this.GetQuery&amp;lt;T&amp;gt;().FindAll().AsQueryable();&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;Exception:&lt;/p&gt;

&lt;p&gt; &lt;span class=&quot;error&quot;&gt;&amp;#91;NullReferenceException: Object reference not set to an instance of an object.&amp;#93;&lt;/span&gt;&lt;br/&gt;
   lambda_method(Closure , User ) +137&lt;br/&gt;
   System.Linq.WhereEnumerableIterator`1.MoveNext() +155&lt;br/&gt;
   System.Linq.Enumerable.First(IEnumerable`1 source) +244&lt;br/&gt;
   lambda_method(Closure ) +322&lt;br/&gt;
   System.Linq.Queryable.First(IQueryable`1 source) +382&lt;br/&gt;
   AuctionCMS.Framework.Services.ClientService.GetByFacebookUserId(String fbUserId) in ClientService.cs:39&lt;/p&gt;

&lt;p&gt;Am I doing something wrong or is this a bug?&lt;/p&gt;</description>
                <environment>VS 2010, MongoDB 2.0.2, Windows 7x64</environment>
        <key id="41770">CSHARP-502</key>
            <summary>NullReferenceException on First() call</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="2" iconUrl="https://jira.mongodb.org/images/icons/priorities/critical.svg">Critical - P2</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="9">Done</resolution>
                                        <assignee username="-1">Unassigned</assignee>
                                    <reporter username="rboarman">Rick B.</reporter>
                        <labels>
                            <label>crash</label>
                            <label>linq</label>
                            <label>query</label>
                    </labels>
                <created>Tue, 19 Jun 2012 19:33:57 +0000</created>
                <updated>Thu, 20 Mar 2014 14:34:18 +0000</updated>
                            <resolved>Thu, 28 Jun 2012 13:36:02 +0000</resolved>
                                    <version>1.4.2</version>
                                                                        <votes>0</votes>
                                    <watches>0</watches>
                                                                                                                <comments>
                            <comment id="134828" author="rboarman" created="Wed, 20 Jun 2012 16:33:42 +0000"  >&lt;p&gt;Got it. Thank you again for the assistance.&lt;/p&gt;</comment>
                            <comment id="134814" author="rstam" created="Wed, 20 Jun 2012 16:05:20 +0000"  >&lt;p&gt;LINQ to MongoDB does not support joins (neither does the underlying MongoDB native query language for that matter).&lt;/p&gt;

&lt;p&gt;You&apos;ll have to read the collections one at a time.&lt;/p&gt;</comment>
                            <comment id="134811" author="rboarman" created="Wed, 20 Jun 2012 16:00:51 +0000"  >&lt;p&gt;Yes, just the exception is being thrown.&lt;/p&gt;

&lt;p&gt;I change the repository code to remove the FindAll() call. However, now I am getting a &quot;SelectMany&quot; is not supported error when I call ToList().&lt;/p&gt;

&lt;p&gt;How should I handle queries like this?&lt;/p&gt;

&lt;p&gt;            var q = from e in this.Repository.All&amp;lt;EmailAddress&amp;gt;()&lt;br/&gt;
                    from u in this.Repository.All&amp;lt;User&amp;gt;()&lt;br/&gt;
                    from c in this.Repository.All&amp;lt;Client&amp;gt;()&lt;br/&gt;
                    where e.Address.ToLowerInvariant() == email.ToLowerInvariant() &amp;amp;&amp;amp; u.Id == e.UserId &amp;amp;&amp;amp; u.Id == c.PrimaryStaffMemberId &amp;amp;&amp;amp; c.Id == u.ClientId&lt;br/&gt;
                    select u;&lt;/p&gt;

&lt;p&gt;            IList&amp;lt;IUser&amp;gt; entities = q.Cast&amp;lt;IUser&amp;gt;().ToList();&lt;/p&gt;

&lt;p&gt;Thanks for the help!&lt;/p&gt;</comment>
                            <comment id="134710" author="craiggwilson" created="Wed, 20 Jun 2012 12:22:20 +0000"  >&lt;p&gt;When you say &quot;crash&quot;, what do you mean?  Was there something else that happened other than the NullReferenceException?&lt;/p&gt;</comment>
                            <comment id="134579" author="rstam" created="Wed, 20 Jun 2012 02:52:36 +0000"  >&lt;p&gt;I&apos;ll look into it, but if you look at your stack trace the exception didn&apos;t come from the C# driver, it came from LINQ. I don&apos;t know if it&apos;s possible to avoid this exception (other than not calling AsQueryable on the result of FindAll...).&lt;/p&gt;</comment>
                            <comment id="134577" author="rboarman" created="Wed, 20 Jun 2012 02:13:18 +0000"  >&lt;p&gt;I&apos;ll make the change tomorrow. Thank you.&lt;/p&gt;

&lt;p&gt;Either way though, the driver shouldn&apos;t have crashed right?&lt;/p&gt;</comment>
                            <comment id="134575" author="rstam" created="Wed, 20 Jun 2012 01:57:48 +0000"  >&lt;p&gt;So GetQuery&amp;lt;T&amp;gt; actually returns a MongoCollection&amp;lt;T&amp;gt;? The name is a bit misleading...&lt;/p&gt;

&lt;p&gt;To repeat my earlier comment, you should call AsQueryable&amp;lt;T&amp;gt; on a MongoCollection&amp;lt;T&amp;gt;, not on the result of calling FindAll. Once you call FindAll you&apos;ve left the LINQ world and are dealing with an IEnumerable&amp;lt;T&amp;gt; result set.&lt;/p&gt;

&lt;p&gt;See the code sample in my previous comment, and let me know if that didn&apos;t answer your question.&lt;/p&gt;</comment>
                            <comment id="134574" author="rboarman" created="Wed, 20 Jun 2012 01:51:13 +0000"  >&lt;p&gt;This is the GetQuery() method and supporting Database property:&lt;/p&gt;

&lt;p&gt;        private MongoDatabase DataBase&lt;br/&gt;
        {&lt;br/&gt;
            get&lt;br/&gt;
            {&lt;br/&gt;
                if (db == null)&lt;/p&gt;
                {
                    db = provider.GetDatabase(this.databaseName);
                }
&lt;p&gt;                return db;&lt;br/&gt;
            }&lt;br/&gt;
        }&lt;/p&gt;

&lt;p&gt;        public MongoCollection&amp;lt;T&amp;gt; GetQuery&amp;lt;T&amp;gt;() where T : class, new()&lt;/p&gt;
        {
            var query = DataBase.GetCollection&amp;lt;T&amp;gt;(typeof(T).Name + &quot;s&quot;);
            return query;
        }

&lt;p&gt;Does this look right? I can send you my whole repository file if you like.&lt;/p&gt;

&lt;p&gt;Thanks!&lt;/p&gt;</comment>
                            <comment id="134474" author="rstam" created="Tue, 19 Jun 2012 19:49:09 +0000"  >&lt;p&gt;You shouldn&apos;t be calling AsQueryable on the result of FindAll. Calling FindAll will result in all documents being returned from the server, so caling AsQueryable doesn&apos;t make sense.&lt;/p&gt;

&lt;p&gt;Also not sure what your GetQuery&amp;lt;T&amp;gt; method does.&lt;/p&gt;

&lt;p&gt;The way to do a LINQ query is to call AsQueryable&amp;lt;T&amp;gt; on the MongoCollection object.&lt;/p&gt;

&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;var query = from u in userCollection.AsQueryable&amp;lt;User&amp;gt;()&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;where u.FbAccount.Id == &quot;832631911&quot;&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;select u;&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;

&lt;p&gt;You can use helper methods if you want as long as they end up doing the same thing as the sample code.&lt;/p&gt;
</comment>
                    </comments>
                    <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hrh7en:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>13999</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>