Linq with DefaultIfEmpty with choice new {}

Linq query with default values. If these values โ€‹โ€‹are not found in the database table, then the default values โ€‹โ€‹from the object should be accepted, and then a new row will be added to this table.

It should be like this, but it doesn't work:

var name_country = (from m in ctx.person
                where (m.name == oPerson.name || m.country == oPerson.country)
                 select new 
                 {
                   m.name, m.country
                 }
                ).DefaultIfEmpty
                               (
                                 oPerson.name,
                                 oPerson.country
                               ).FirstOrDefault();

      

How to set default values โ€‹โ€‹in DefaultIfEmpty ???

New edit: This is what I want to do as one request:

string name = (from m in ctx.person
                where (m.name == oPerson.name || m.country == oPerson.country)
                 select  
                   m.name
                ).DefaultIfEmpty
                               (
                                 oPerson.name,
                               ).FirstOrDefault();
string country = (from m in ctx.person
                where (m.name == oPerson.name || m.country == oPerson.country)
                 select 

                  m.country

                ).DefaultIfEmpty
                               (
                                 oPerson.country
                               ).FirstOrDefault();

      

+3


source to share


3 answers


var name_country = (from m in ctx.person
            where (m.name == oPerson.name || m.country == oPerson.country)
             select new 
             {
               m.name, m.country
             }
            ).DefaultIfEmpty
                           (new {
                             oPerson.name,
                             oPerson.country
                           }).First();

      

This will work as long as the layout member is identical .
This works because anonymous types are anonymous at runtime ... Please read the MSDN entry for more information on this topic:

If two or more anonymous object initializers in an assembly specify a sequence of properties that are in the same order and that have the same name and type, the compiler treats the objects as instances of the same type. They are of the same type of compiler-generated information.



Also, I would rather go for ??

...

var name_country = (from m in ctx.person
                    where (m.name == oPerson.name || m.country == oPerson.country)
                    select new 
                    {
                        m.name,
                        m.country
                    }).FirstOrDefault() ?? new {
                        oPerson.name,
                        oPerson.country
                    };

      

edit: here's a working fiddle

+3


source


Are you looking for this overload DefaultIfEmpty

public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
    this IEnumerable<TSource> source,
    TSource defaultValue
)

      



You should create a new anonymous object, set its properties and pass it to the constructor.

+1


source


Assuming you have a class Person

that looks like

public class Person
{
    public string Name { get; set; }
    public string Country { get; set; }
}

      

What you want to do is create a new instance Person

(which will automatically set default values โ€‹โ€‹for each specific property type) if it doesn't come back from your DB query, for example.

var name_country = (from m in ctx.person
                    where (m.name == oPerson.name || m.country == oPerson.country)
                    select new Person
                    {
                        Name = m.name, 
                        Country = m.country
                    }).FirstOrDefault() ?? new { oPerson.name, oPerson.country };

      


Just figured out that you want to default to using fields from an instance oPerson

instead of a new instance. Thus, assuming that is oPerson

also an anonymous object with the same element structure, you could do

var name_country = (from m in ctx.person
                    where (m.name == oPerson.name || m.country == oPerson.country)
                    select new
                    {
                        m.name, 
                        m.country
                    })
                    .DefaultIfEmpty(aPerson)
                    .FirstOrDefault();

      

+1


source







All Articles