What Does Linq Return When the Results Are Empty

What does LINQ return when the results are empty

It will return an empty enumerable. It won't be null. You can sleep sound :)

How to return null when the Linq query returns empty value?

Use ?. and ?[] null-conditional Operators. It tests the value of the left-hand operand for null before performing a member access (?.) or index (?[]) operation; returns null if the left-hand operand evaluates to null.

GetList().SingleOrDefault(x => x.Key == "MyKey")?.MyValue;

how to know if my linq query returns null

You can realise the result as a list:

var myQuery = (from Q in myDataContext select Q.Name).ToList();

Now you can check the number of items:

if (myQuery.Count > 0) ...

You could also use the Count() method on the original query, but then you would be running the query twice, once to count the items, and once to use them.

If a linq query returns empty does it return null?

It will return an IEnumerable<ActivityLog> with no elements.

To check if there are any elements, use the Any() method:

if(!logs.Any())
{
Console.WriteLine("No elements found.");
}

Also note that as you've written it, vm.logs will be lazily evaluated, that is, it won't be fetched from the database until it is used. If you first do a .Any() and then later access the contents of the query, there will be two queries executed in the database. To avoid that, materialize (force the query to execute) by adding a ToList() to the query:

 vm.logs = (from l in db.ActivityLogs
orderby l.Time
select l).Take(2).ToList();

Return an empty collection when Linq where returns nothing

Where returns an empty sequence if there are no matches; this is a perfectly valid sequence (not null). The only way you'd get a null is if you call FirstOrDefault or SingleOrDefault.

Are you sure the bug is where you think it is?

int?[] nums = { 1, 3, 5 };
var qry = nums.Where(i => i % 2 == 0);
Console.WriteLine(qry == null); // false
Console.WriteLine(qry.Count()); // 0
var list = qry.ToList();
Console.WriteLine(list.Count); // 0
var first = qry.FirstOrDefault();
Console.WriteLine(first == null); // true

Linq query to return results when an ICollection is empty.

Can anybody tell me what I'm doing wrong?

The line

from pos in o.Positions

creates an inner join between job (one) and job positions (zero or many). The effect is that it will multiply the job records when there are many positions and will filter jobs w/o related positions (regardless of your where clause).

To make it work, you need to remove that statement and use Any based Where criteria on the many side (the equivalent of the SQL EXISTS (subquery) clause):

var feedQuery = 
from o in _context.Jobs
where !o.Positions.Any() || o.Positions.Any(pos => positions.Contains(pos.Id))
select o;

Return null from ToList() if list is empty?

I don't know what are your requirements to return null when your list is empty, but you can write your extension method to do that:

static class MyExtensions
{
public static List<T> NullIfEmpty<T>(this List<T> list) =>
list.Any() ? list : null;

// this will be more optimal solution
public static List<T> ToListOrNullIfEmpty<T>(this IEnumerable<T> collection) =>
collection.Any() ? collection.ToList() : null;

}

Usage:

list.Where(x => x == 3).ToList().NullIfEmpty();

or

list.Where(x => x == 3).ToListOrNullIfEmpty();

LINQ to return empty result

There is no need to hit database. You can use following:

private IQueryable <EMPLOYEE> getEmployee()
{
return Enumerable.Empty<EMPLOYEE>().AsQueryable();
}

LINQ to return null if an array is empty

If you're doing this a lot, you could write an extension method:

public static class IEnumerableExt
{
public static T[] ToArrayOrNull<T>(this IEnumerable<T> seq)
{
var result = seq.ToArray();

if (result.Length == 0)
return null;

return result;
}
}

Then your calling code would be:

var data = coll.Select(s => s.x).ToArrayOrNull();


Related Topics



Leave a reply



Submit