Linq to SQL: .FirstOrDefault () is not applicable to select a new {...}

I just asked this question . Which led me to a new question :)

Until now, I used the following Linq to SQL material selection template to process the 0 "rows" returned by the query:

var person = (from p in [DataContextObject].Persons where p.PersonsID == 1 select new p).FirstOrDefault(); if (person == null) { // handle 0 "rows" returned. } 

But I can not use FirstOrDefault() when I do this:

 var person = from p in [DataContextObject].Persons where p.PersonsID == 1 select new { p.PersonsID, p.PersonsAdress, p.PersonsZipcode }; // Under the hood, this pattern generates a query which selects specific // columns which will be faster than selecting all columns as the above // snippet of code does. This results in a performance-boost on large tables. 

How to check for the presence of 0 "rows" returned by a query using the second pattern?







UPDATE:

I think my build failed because I'm trying to assign the result of the query to a variable ( this._user ) declared with type [DataContext].User .

 this._user = (from u in [DataContextObject].Users where u.UsersID == [Int32] select new { u.UsersID }).FirstOrDefault(); 

Compilation error: It is not possible to implicitly convert the type "Anonymous type No. 1" to "[DataContext] .User".

Any thoughts on how I can get around this? Should I create my own object?

+4
source share
5 answers

As for your UPDATE: you need to either create your own type, or change this._user as int, or select the whole object, not just specific columns.

+1
source

Why can you keep doing something? Does this give you an error message?

 var person = (from p in [DataContextObject].Persons where p.PersonsID == 1 select new { p.PersonsID, p.PersonsAdress, p.PersonsZipcode }).FirstOrDefault(); if (person == null) { // handle 0 "rows" returned. } 

It is still a reference object, just like your actual object, it is just anonymous, so you don't know the actual type before compiling the code.

+13
source

Update:

Now I see what you actually asked! Sorry, my answer is no longer applicable. I thought you would not get a null value when it is empty. The accepted answer is correct, if you want to use the object out of scope, you need to create a new type and just use New MyType (...). I know that DevEx RefactorPro has refactoring for this, and I think resharper also does.

Call .FirstOrDefault (null) as follows:

 string[] names = { "jim", "jane", "joe", "john", "jeremy", "jebus" }; var person = ( from p in names where p.StartsWith("notpresent") select new { Name=p, FirstLetter=p.Substring(0,1) } ) .DefaultIfEmpty(null) .FirstOrDefault(); MessageBox.Show(person==null?"person was null":person.Name + "/" + person.FirstLetter); 

It does the trick for me.

+2
source
 if (person.Any()) /* ... */; 

OR

 if (person.Count() == 0) /* ... */; 
+1
source

You can use FirstOrDefault . Just

 var PersonFields = (...).FirstOrDefault() 

PersonFields will be null or an object with the properties you created.

0
source

Source: https://habr.com/ru/post/1277736/


All Articles