innovasoft
asked on
FirstOrDefault() in Where-Statement (nhibernate, linq)
Hi everybody
I got this project-class with a list of status (already in the right order)
the mapping is like this
Now I would like to search a project where the current status (the first in the list) has Type = 2. I thought I could easily handle this like this:
But this just throws following exception (unfortunately I only got the german version of it)
Eine Ausnahme vom Typ "Antlr.Runtime.NoViableAlt Exception" wurde ausgelöst. [.Count[OSS.Domain.Project ](.Where[O SS.Domain. Project](N Hibernate. Linq.NhQue ryable`1[O SS.Domain. Project], Quote((g, ) => (Equal(Convert(.FirstOrDef ault[OSS.D omain.Proj ectStatus] (g.Status, ).Type.Id), p1))), ), )]
Does anyone know how to fix this?
Thank you very much!
I got this project-class with a list of status (already in the right order)
public class Project
{
public virtual int Id { get; set; }
public virtual IList<ProjectStatus> Status { get; set; }
}
public class ProjectStatus
{
public virtual int Id { get; set; }
public virtual Project Project { get; set; }
public virtual int Type { get; set; }
public virtual DateTime Creationdate { get; set; }
}
the mapping is like this
public ProjectMapping()
{
Table("Projects");
Id(x => x.Id, "ProjectId");
HasMany(x => x.Status).Table("ProjectStatus").KeyColumn("ProjectId").OrderBy("Creationdate DESC").Cascade.All();
}
public ProjectStatusMapping()
{
Table("ProjectStatus");
Id(x => x.Id, "ProjectStatusId");
Map(x => x.Creationdate);
Map(x => x.Typ);
References<Project>(x => x.Project, "ProjectId");
}
Now I would like to search a project where the current status (the first in the list) has Type = 2. I thought I could easily handle this like this:
_projectRepository.Table.Where(p => p.Status.FirstOrDefault() != null && p.Status.Type == 2).FirstOrDefault();
But this just throws following exception (unfortunately I only got the german version of it)
Eine Ausnahme vom Typ "Antlr.Runtime.NoViableAlt
Does anyone know how to fix this?
Thank you very much!
Well this logic shouldn't even compile:
...and even if it had it wouldn't have produced expected results, unless there was only one element in the collection. The reason is that the "p.Status.FirstOrDefault() " would return the first element of the collection--regardless of what Type it had--and the "p.Status.Type == 2"--barring the fact that the syntax is incorrect--would be independent of the earlier "p.Status.FirstOrDefault() ".
Does you newest query (the bottom one) raise an exception, or does it just not return expected results?
_projectRepository.Table.Where(p => p.Status.FirstOrDefault() != null && p.Status.Type == 2).FirstOrDefault();
...and even if it had it wouldn't have produced expected results, unless there was only one element in the collection. The reason is that the "p.Status.FirstOrDefault()
Does you newest query (the bottom one) raise an exception, or does it just not return expected results?
ASKER
thank you for your answer!
ups... I made a mistake when I put the code in the textfield (i changed some variable-names so that you can understand it better). But it actually looks like this
I'm sorry for that
and this still throws the same exception as I wrote in the first post
ups... I made a mistake when I put the code in the textfield (i changed some variable-names so that you can understand it better). But it actually looks like this
_projectRepository.Table.Where(p => p.Status.FirstOrDefault() != null && p.Status.FirstOrDefault().Type == 2).FirstOrDefault();
I'm sorry for that
and this still throws the same exception as I wrote in the first post
I'm not sure I can be of further assistance since I am not familiar with NHibernate. When looking up the details of a NoViableAltException it seems the causes vary. From what I can see, it sounds like you might be using an invalid construct as far as NHibernate is concerned. To me, it seems akin to using foreign functions in LINQ-to-SQL (e.g. trying to use DateTime.Parse within a query where there is no equivalent SQL function to DateTime.Parse). The only thing in the query that seems plausible is the FirstOrDefault, but I think it would be weird that that particular function would be unsupported.
Certainly hold out for responses from the other experts : )
Certainly hold out for responses from the other experts : )
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I didn't really get a nice solution. This one is only a workaround and I wanted to let you all know about that
ASKER
Open in new window
But this just returns any status with Type==2 and not only the latest one. So I tried this
Open in new window
But this fails again :(