2

I have defined the following entity classes for my LINQ query:

public class Application
{
    public Application() { }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public System.DateTime DateTimeCreated { get; set; }
    public System.DateTime? DateTimeModified { get; set; }
    public Employee CreatedBy { get; set; }
    public Employee ModifiedBy { get; set; }
}

public class Employee
{
    public Employee() { }

    public string EmployeeID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

I have created the following query to create an Application object and trying to create an Employee entity for the CreatedBy and 'ModifiedBy' properties. Sometimes, the ModifiedBy column can contain null and I want to set the ModifiedBy property to null.

var query = from a in context.Applications
            join u1 in context.Employees on a.CreatedBy equals u1.Employee_ID.Trim()
            join u2 in context.Employees on a.ModifiedBy equals u2.Employee_ID.Trim()  
            where a.ApplicationId == applicationId
            select new Entity.Application
            {
                Id = a.ApplicationId,
                Name = a.ApplicationName,
                Description = a.ApplicationDesc,
                DateTimeCreated = a.DateTimeCreated,
                CreatedBy = new Entity.Employee{ EmployeeID = a.CreatedBy, FirstName = u1.First_Name, LastName = u1.Last_Name },
                DateTimeModified = a.DateTimeModified ?? null,
                ModifiedBy = (a.ModifiedBy != null) ? new Entity.Employee { EmployeeID = a.ModifiedBy, FirstName = u2.First_Name, LastName = u2.Last_Name } : (Entity.Employee) null,
             };  

When I debug the query above, I get the following error:

Type of conditional expression cannot be determined because there is no implicit conversion between 'Employee' and 'Application'

How do I resolve this error?

3
  • what is the field/property ApplicationId - should that be just Id? Commented Mar 12, 2014 at 15:55
  • So the query above works if you remove this line? ModifiedBy = (a.ModifiedBy != null) ? new Entity.Employee { EmployeeID = a.ModifiedBy, FirstName = u2.First_Name, LastName = u2.Last_Name } : (Entity.Employee) null, Commented Mar 12, 2014 at 15:56
  • @nate - yes, I get past that error when I removed the line referenced in your comment. Commented Mar 12, 2014 at 16:01

3 Answers 3

3

It may not directly address an error you're getting, but you don't need that query at all, because you have navigation properties set. Use Include instead and it should work just fine - EF will join necessary joins for you:

var query context.Applications
                 .Include("ModifiedBy")
                 .Include("CreatedBy")
                 .Where(a => a.ApplicationId == applicationId);
Sign up to request clarification or add additional context in comments.

1 Comment

When I try to look at the results view of this query, I get the following message in debug mode: Children could not be evaluated.
2

A couple things I am noticing

 join u1 in context.Employees on a.CreatedBy equals u1.Employee_ID.Trim()
 join u2 in context.Employees on a.ModifiedBy equals u2.Employee_ID.Trim()

You can't join like this, as CreateBy and ModifiedBy are of type Employee, not string

Also, have a look at this:

 (Entity.Employee) null

You can't cast null to Employee. You might want to use the type's default value in future:

 default(Entity.Employee)

UPDATE

As pointed out in the comments, it is legal to cast null to Entity.Employee, but since you end up with null anyways there is not much point to the exercise. default(Entity.Employee) also results in null, since that's the default value for reference types, but default could provide a different value for various other types, which can sometimes be useful.

4 Comments

I agree with join, but you definitely can cast null to Entity.Employee.
Indeed! I must have brought some assumptions forward from my pre-C# days!
The CreatedBy and ModifiedBy columns in the context Applications table are defined as string. I am doing the join to get the employee information from the context Employee view
@MichaelKniskern Why would those fields of type Employee in your Application model?
0

After some additional research, here is the revised code snippet that ended up working for me:

var result = (from a in context.Applications
               join u1 in context.Employee on a.CreatedBy equals u1.Employee_ID.Trim()
               join u2 in context.Employee on a.ModifiedBy equals u2.Employee_ID.Trim() into us
               from u2 in us.DefaultIfEmpty()
               where a.ApplicationId == applicationId
               select new Entity.Application()
               {
                   Id = a.ApplicationId,
                   Name = a.ApplicationName,
                   Description = a.ApplicationDesc,
                   DateTimeCreated = a.DateTimeCreated,
                   CreatedBy = new Entity.Employee
                   {
                       EmployeeID = a.CreatedBy,
                       FirstName = u1.First_Name,
                       LastName = u1.Last_Name
                   },
                   DateTimeModified = a.DateTimeModified ?? null,
                   ModifiedBy = new Entity.Employee
                   {
                       EmployeeID = a.ModifiedBy ?? string.Empty,
                       FirstName = u2.First_Name ?? string.Empty,
                       LastName = u2.Last_Name ?? string.Empty
                   }
               }).Single();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.