1

I'm starting with a SQL query that selects against a view & looks like this:

SET @id = ...
SET @variableDate = ...
SELECT Id,
        dbo.fnGreaterDateTime(ViewDate,@variableDate) AS GreaterDate,
    FROM vwExample
    WHERE Id = @id

The SQL function fnGreaterDateTime works as you would expect, returning the greater of the two values passed in.

I'm having trouble converting this to a LINQ query when using EF & RIA services. In my Domain Service, I would like to be able to do something like the following:

public IQueryable<ExampleViewResult> GetExampleViewResults(int id, DateTime variableDate)
{

    var query = from r in this.ObjectContext.ExampleViewResults
        where r.Id == id
        select new ExampleViewResult
          { 
            Id = r.Id,
            ViewDate = (r.ViewDate > variableDate) ? r.ViewDate : variableDate
          }
    return query;
}

But when I call this method I receive an error that says "The entity or complex type ExampleViewResult cannot be constructed in a LINQ to Entities query".

I tried following some advice over in this thread, but when I have the domain service method return a list of DTOs instead, the auto-code generation doesn't include the method for use in my domain context.

Any ideas?

Community
  • 1
  • 1
Chris Trombley
  • 2,232
  • 1
  • 17
  • 24
  • what happens if you do var listResults = this.OjbectContext.ExampleViewResults.ToList(); then var query = from r in listResults... – Mark W Jul 18 '11 at 19:05
  • This can't be the actual code you have, as this will not compile (missing ;). This makes me think you removed parts that you thought were not relevant, and those that could be the source of the problem. Please post the complete code. – cadrell0 Jul 18 '11 at 20:39
  • @cadrell0 - I did remove some parts that were just noise in the example. Which problem are you referring to? I'm looking for an alternate means of writing the query. – Chris Trombley Jul 18 '11 at 21:40
  • @Chris What I am trying to say is because LINQ uses deferred execution, perhaps that noise is the root of the problem. – cadrell0 Jul 19 '11 at 14:46

1 Answers1

0

see this question

The entity cannot be constructed in a LINQ to Entities query

you just need to project to an anonymous type or a DTO

Community
  • 1
  • 1
BlackTigerX
  • 6,006
  • 7
  • 38
  • 48