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();
source to share
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
source to share
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.
source to share
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();
source to share