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 you add a DefaultIfEmpty so that when there's no vote I'd get a default value?
...
So my situation is that I have a linq-to-sql model that does not allow dates to be null in one of my tables. This is intended, because the database does not allow nulls in that field. My problem, is that when I try to write a Linq query with this model, I cannot do a left join with that table anymore because the date is not a 'nullable...
var defaultRole=ZAssociateRoles.Single(r=>r.RoleName=="Associate");
var allAssociates=(from a in ZAssociates
join arm in Associate_role_maps
on a.UserName equals arm.UserName into leftArm
from la in leftArm.DefaultIfEmpty()
join r in ZAssociateRoles
on la.AssociateRoleId equals r.AssociateRoleId into leftOuterRole
...
I'm new to linq and linq to entities so I might have gone wrong in my assumptions, but I've been unknowingly trying to use DefaultIfEmpty in L2E.
For some reason if I turn the resultset into a List, the Defaultifempty() works I don't know if I've inadvertantly crossed over into Linq area. The code below works, can anyone tell me why? ...
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 (var item in query)
{
Console.WriteLine(item.ToString());
}
...
from a in mainDoc.XPathSelectElements("//AssembliesMetrics/Assembly/@Assembly")
let aVal=a.Value
where aVal.IsNullOrEmpty( )==false&&aVal.Contains(" ")
select aVal.Substring(0, aVal.IndexOf(' '))
into aName
...