Questions tagged [defaultifempty]
40 questions
13
votes
5 answers
How do you return a default value if a LINQ to entities query returns no values
In a LINQ to entities expression like this:
var vote = (from vote in db.Vote where
vote.Voter.Id == user.Id
select v).FirstOrDefault();
How do you add a DefaultIfEmpty value so that when there's no vote I'd get a default value?

Pablo Fernandez
- 279,434
- 135
- 377
- 622
6
votes
1 answer
Linq, OrderByDescending, First, and the nefarious DefaultIfEmpty
Hopefully this is a simple matter of me not understanding something basic. Below are two Linq statements from an application I'm working on.
EDMXModel.Classes.Period p1 = entities.Periods.DefaultIfEmpty(null).OrderByDescending(ap =>…

AEberhard
- 77
- 4
6
votes
2 answers
ASP Session variables: Is "" same as IsEmpty?
In ASP an uninitialized Session variable Is Empty. I know that the correct way to check for a Session value, and remove a value, is the following:
IF NOT IsEmpty(Session("myVar")) THEN
' Go ahead and use Session("myVar")
...
' Now if we're…

feetwet
- 3,248
- 7
- 46
- 84
6
votes
2 answers
LINQ NullReferenceException on DefaultIfEmpty
I'm looking for a solution to the problem of having the DefaultIfEmpty() extension method not picking up null values when used in a LINQ outer join.
Code as follows:
var SummaryLossesWithNets = (from g in SummaryLosses
…

Richard Todd
- 2,406
- 5
- 32
- 40
5
votes
2 answers
How to define an empty array of DataFrames in Julia?
I want to generate an empty array of dataframes that will be filled later in the code, but I have not figured out how to do it. Any help would be appreciated!
I have tried a standard way of defining an empty array.
julia> df =…

JPi
- 127
- 6
4
votes
2 answers
Entity Framework join nullable join columns
My Devart Entity Framework provider takes the following linq to entities.
from p in context.BEAT_CONTACT
join cl in context.COMPASS_LOCATIONS
on p.GAZETEER_KEY equals cl.GAZETTEER_KEY
//on new { bcdid = p.GAZETEER_KEY.Value }
//equals new { bcdid =…

Dilbert
- 53
- 1
- 8
4
votes
3 answers
Linq with DefaultIfEmpty with select new {}
Linq query with default values.
If in DB Table this values are not found, than default values from object should be taken, and later on new row will be added to this table.
It should go like this, but this does not work:
var name_country = (from m…

3m1n4
- 53
- 1
- 1
- 8
3
votes
1 answer
DefaultIfEmpty() in LINQ to SQL join causing duplicates
Code:
var cons = from c in dc.Consignments
join p in dc.PODs ON c.ID equals p.Consignment into pg
from p in pg.DefaultIfEmpty()
...(other joins)...
select new {
...
PODs = pg
...
}
Basically, I want one row to be selected for each Consignment, and…

Chris
- 7,415
- 21
- 98
- 190
3
votes
2 answers
DefaultIfEmpty doesn't work
Why is array still null after queried by DefaultIfEmpty ?
class Program
{
static void Main(string[] args)
{
Program[] array = new Program[5];
Program[] query = array.DefaultIfEmpty(new Program()).ToArray();
foreach…

Freshblood
- 6,285
- 10
- 59
- 96
2
votes
2 answers
Understanding DefaultIfEmpty in LINQ
I don't understand how DefaultIfEmpty method works. It is usually used to be reminiscent of left-outer join in LINQ.
DefaultIfEmpty() method must be run on a collection.
DefaultIfEmpty() method cannot be run on null collection reference.
A code…

Soner from The Ottoman Empire
- 18,731
- 3
- 79
- 101
1
vote
1 answer
How to generate empty value from aggregate results with Mongodb
First of all the function :
static async getInitRegistrationMetric(account) {
// we want only full day so we exclude current date
const exclude = DateTime.now()
.setZone('utc')
.startOf('day')
.toISODate();
// this count the number of client…

Arnaud Dev
- 53
- 5
1
vote
1 answer
jq: pick null instead of empty
I've created this filter expression:
def pick_extension($url):
.extension[] | select( .url == $url);
[
( pick_extension( "http://hl7.org/fhir/StructureDefinition/patient-nationality" ) | .url, ( .extension[0] | .url, (…

Jordi
- 20,868
- 39
- 149
- 333
1
vote
2 answers
DefaultIfEmpty for LINQ to a DataTable?
I have a LINQ query written to pull at least one row from a datatable as follows:
var generalQuery =
from contact in contacts.AsEnumerable().DefaultIfEmpty()
where contact.Field("CONTACT_TYPE").ToUpper() == "AGENT"
select…

SpaceCowboy74
- 1,367
- 1
- 23
- 46
1
vote
0 answers
How can I save space in array when init with null?
I have a linq syntax to get elemnts from XML,
this is the XML syntax:
FIRST
THIRD
I want to init a string array with the products :
string[]…

user1012506
- 2,048
- 1
- 26
- 45
1
vote
2 answers
Why is this defaultIfEmpty choosing ienumerable instead of string?
from a in mainDoc.XPathSelectElements("//AssembliesMetrics/Assembly/@Assembly")
let aVal=a.Value
where aVal.IsNullOrEmpty( )==false&&aVal.Contains(" ")
select…

Maslow
- 18,464
- 20
- 106
- 193