<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:37:03 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-507] System.NullReferenceException on Empty String comparison</title>
                <link>https://jira.mongodb.org/browse/CSHARP-507</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;p&gt;The following code gives us a NullReferenceException in FirstOrDefault() implementation:&lt;/p&gt;

&lt;p&gt;collection.AsQueryable().Where(d =&amp;gt; d.StringField == String.Empty).Select(d =&amp;gt; d.SomeField).FirstOrDefault()&lt;/p&gt;</description>
                <environment></environment>
        <key id="41893">CSHARP-507</key>
            <summary>System.NullReferenceException on Empty String comparison</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</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="robert@mongodb.com">Robert Stam</assignee>
                                    <reporter username="zaidmasud">Zaid Masud</reporter>
                        <labels>
                            <label>linq</label>
                            <label>linq,query</label>
                    </labels>
                <created>Wed, 20 Jun 2012 15:20:49 +0000</created>
                <updated>Thu, 20 Mar 2014 14:34:22 +0000</updated>
                            <resolved>Wed, 4 Jul 2012 03:21:56 +0000</resolved>
                                    <version>1.5</version>
                                    <fixVersion>1.6</fixVersion>
                                                        <votes>0</votes>
                                    <watches>1</watches>
                                                                                                                <comments>
                            <comment id="139519" author="zaidmasud" created="Wed, 4 Jul 2012 08:49:08 +0000"  >&lt;p&gt;Thanks... I would consider an additional unit test for SingleOrDefault() as well although your fix seems to apply for both.&lt;/p&gt;</comment>
                            <comment id="139453" author="rstam" created="Wed, 4 Jul 2012 03:21:47 +0000"  >&lt;p&gt;Zaid, thanks for the additional information. That really helped in reproducing this and in coming up with a fix.&lt;/p&gt;

&lt;p&gt;The exception is only thrown if FirstOrDefault is called after a projection that returns a struct (bool is a struct), which is why we hadn&apos;t seen this before.&lt;/p&gt;</comment>
                            <comment id="139447" author="auto" created="Wed, 4 Jul 2012 02:46:41 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{u&apos;date&apos;: u&apos;2012-07-03T14:44:59-07:00&apos;, u&apos;name&apos;: u&apos;Robert Stam&apos;, u&apos;email&apos;: u&apos;robert@10gen.com&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/CSHARP-507&quot; title=&quot;System.NullReferenceException on Empty String comparison&quot; class=&quot;issue-link&quot; data-issue-key=&quot;CSHARP-507&quot;&gt;&lt;del&gt;CSHARP-507&lt;/del&gt;&lt;/a&gt;: Support FirstOrDefault when LINQ query includes a projection to a struct and doesn&apos;t match any documents (which was failing because null can&apos;t be cast to a struct).&lt;br/&gt;
Branch: master&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-csharp-driver/commit/3d8d92a60ed62055201c1b11de7b715e71474392&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-csharp-driver/commit/3d8d92a60ed62055201c1b11de7b715e71474392&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="139058" author="zaidmasud" created="Tue, 3 Jul 2012 14:43:47 +0000"  >
&lt;p&gt;System.NullReferenceException was unhandled&lt;br/&gt;
  Message=Object reference not set to an instance of an object.&lt;br/&gt;
  Source=MongoDB.Driver&lt;br/&gt;
  StackTrace:&lt;br/&gt;
       at MongoDB.Driver.Linq.MongoQueryProvider.Execute&lt;span class=&quot;error&quot;&gt;&amp;#91;TResult&amp;#93;&lt;/span&gt;(Expression expression) in C:\Users\ZAID\Documents\GitHub\mongo-csharp-driver\Driver\Linq\MongoQueryProvider.cs:line 130&lt;br/&gt;
       at System.Linq.Queryable.FirstOrDefault&lt;span class=&quot;error&quot;&gt;&amp;#91;TSource&amp;#93;&lt;/span&gt;(IQueryable`1 source)&lt;br/&gt;
       at MongoDriverBug.Program.Main() in C:\MongoDriverBug\MongoDriverBug\Program.cs:line 24&lt;br/&gt;
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)&lt;br/&gt;
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)&lt;br/&gt;
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()&lt;br/&gt;
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)&lt;br/&gt;
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)&lt;br/&gt;
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)&lt;br/&gt;
       at System.Threading.ThreadHelper.ThreadStart()&lt;br/&gt;
  InnerException: &lt;/p&gt;</comment>
                            <comment id="139057" author="zaidmasud" created="Tue, 3 Jul 2012 14:39:52 +0000"  >&lt;p&gt;using System;&lt;br/&gt;
using System.Linq;&lt;br/&gt;
using MongoDB.Driver;&lt;br/&gt;
using MongoDB.Bson;&lt;br/&gt;
using MongoDB.Driver.Linq;&lt;/p&gt;

&lt;p&gt;namespace MongoDriverBug&lt;br/&gt;
{&lt;br/&gt;
    class Program&lt;br/&gt;
    {&lt;br/&gt;
        public class Document&lt;br/&gt;
        {&lt;br/&gt;
            public ObjectId Id &lt;/p&gt;
{ get; set; }&lt;br/&gt;
            public bool BoolField { get; set; }
&lt;p&gt;        }&lt;/p&gt;

&lt;p&gt;        static void Main()&lt;/p&gt;
        {
            MongoDatabase db = MongoServer.Create(&quot;mongodb://localhost/?safe=true&quot;).GetDatabase(&quot;test&quot;);
            db.DropCollection(&quot;Document&quot;);

            IQueryable&amp;lt;Document&amp;gt; documents = db.GetCollection&amp;lt;Document&amp;gt;(&quot;Document&quot;).AsQueryable();

            Console.WriteLine(documents
                .Where(d =&amp;gt; d.Id == ObjectId.GenerateNewId())
                .Select(d =&amp;gt; d.BoolField)
                .FirstOrDefault());

            Console.ReadKey();
        }
&lt;p&gt;    }&lt;br/&gt;
}&lt;/p&gt;</comment>
                            <comment id="139048" author="zaidmasud" created="Tue, 3 Jul 2012 14:31:43 +0000"  >&lt;p&gt;This turned out to be a slightly different issue.&lt;/p&gt;

&lt;p&gt;The empty string was causing no matches.&lt;/p&gt;

&lt;p&gt;So to re-phrase, the exception is thrown when there is a Where().Select().FirstOrDefault when the where clause results in no matches returned.&lt;/p&gt;

&lt;p&gt;i.e.&lt;/p&gt;

&lt;p&gt;collection.AsQueryable().Where(d =&amp;gt; d.Id == ObjectId.GenerateNewId()).Select(d =&amp;gt; d.BoolField).FirstOrDefault();&lt;/p&gt;

&lt;p&gt;In our specific case, the field being selected is a boolean type but I&apos;m not sure whether that is relevant.&lt;/p&gt;</comment>
                            <comment id="137505" author="craiggwilson" created="Thu, 28 Jun 2012 13:52:20 +0000"  >&lt;p&gt;I cannot reproduce this.  If you could provide a stack trace of the error and a sample program to reproduce it, that would be immensely helpful.  My program is below:&lt;/p&gt;

&lt;p&gt;using System;&lt;br/&gt;
using System.Collections.Generic;&lt;br/&gt;
using System.Linq;&lt;br/&gt;
using System.Text;&lt;br/&gt;
using MongoDB.Driver;&lt;br/&gt;
using MongoDB.Driver.Builders;&lt;br/&gt;
using MongoDB.Bson;&lt;br/&gt;
using MongoDB.Driver.Linq;&lt;br/&gt;
using System.Collections.ObjectModel;&lt;br/&gt;
using MongoDB.Bson.Serialization;&lt;/p&gt;

&lt;p&gt;namespace GeneralConsole&lt;br/&gt;
{&lt;br/&gt;
    class Program&lt;br/&gt;
    {&lt;br/&gt;
        public class Person&lt;br/&gt;
        {&lt;br/&gt;
            public ObjectId Id &lt;/p&gt;
{ get; set; }&lt;br/&gt;
            public string Firstname { get; set; }
&lt;p&gt;            public string Lastname &lt;/p&gt;
{ get; set; }
&lt;p&gt;        }&lt;/p&gt;

&lt;p&gt;        static void Main(string[] args)&lt;br/&gt;
        {&lt;br/&gt;
            // Construct Objects&lt;br/&gt;
            Person person = new Person &lt;/p&gt;
{ Firstname = string.Empty, Lastname = &quot;funny&quot; }
&lt;p&gt;;&lt;/p&gt;

&lt;p&gt;            // Connect to Mongo&lt;br/&gt;
            var server = MongoServer.Create(&quot;mongodb://localhost/?safe=true&quot;);&lt;br/&gt;
            var db = server.GetDatabase(&quot;test&quot;);&lt;br/&gt;
            db.DropCollection(&quot;parents&quot;);&lt;br/&gt;
            var collection = db.GetCollection&amp;lt;Person&amp;gt;(&quot;parents&quot;);&lt;br/&gt;
            collection.Save(new Person &lt;/p&gt;
{ Firstname = string.Empty, Lastname = null }
&lt;p&gt;);&lt;br/&gt;
            collection.Save(new Person &lt;/p&gt;
{ Firstname = null, Lastname = string.Empty }
&lt;p&gt;);&lt;/p&gt;

&lt;p&gt;            var parentName = collection.AsQueryable()&lt;br/&gt;
                .Where(x =&amp;gt; x.Firstname == string.Empty)&lt;br/&gt;
                .Select(x =&amp;gt; x.Firstname)&lt;br/&gt;
                .FirstOrDefault();&lt;/p&gt;

&lt;p&gt;            parentName = collection.AsQueryable()&lt;br/&gt;
                .Where(x =&amp;gt; x.Lastname == string.Empty)&lt;br/&gt;
                .Select(x =&amp;gt; x.Lastname)&lt;br/&gt;
                .FirstOrDefault();&lt;/p&gt;

&lt;p&gt;            Console.ReadKey();&lt;br/&gt;
        }&lt;br/&gt;
    }&lt;br/&gt;
}&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|hrh7dz:</customfieldvalue>

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