Page view counter

LINQ Issue: no supported translation to SQL

Last post 07-04-2008 3:34 PM by chris.wall. 1 replies.

Sort Posts:

  • LINQ Issue: no supported translation to SQL

    07-03-2008, 10:45 PM
    • Loading...
    • chris.wall
    • Joined on 07-03-2008, 10:17 PM
    • Posts 2
    • Points 0

    I apologize in advance if this is the wrong forum to post this in, didn't see any forum's description mentioning LINQ.

    I am getting the following exception:

    The member '[MyBusinessClass].ID' has no supported translation to SQL.

    For starters, this isn't even a Linq to Sql class. Am I wrong to assume that I can plug an object into an IQueryable collection and query it as I would a database?

    Edit: When I try the following line of code everything works fine
    this._repository.GetNewsPosts().ToList().AsQueryable().WithID(someGuid);
    But this still does not:
    this._repository.GetNewsPosts().WithID(someGuid);
    See below for the meat of the GetNewsPosts and WithID methods.

    The whole of what is happening makes it more odd.  I am running some unit tests on a website I am working on.  I wanted to do it all in LINQ to learn the new language features in 3.5.  I have a Service class and a Repository class.  The service has convenience methods like GetItemsById(guid id) which maps to a call to repository that gets an IQueryable of business objects (NOT the objects in my dbml file, but a translation of one of those) using an extension method (WithId(this IQueryable<BizObj> objects, guid id))  It's all set up with interfaces (IService and IRepository) for my IoC container. When I unit test the Service using a mock repository everything works fine.  When I unit test the actual repository the WithID method throws an exception.  Here are some snippets of the code I am working with:

    ///This is the extension method
    public static IQueryable WithID(this IQueryable businessObjects, Guid postID)
    {
    return from n in businessObjects
    where n.ID == postID
    select n;
    }

    ///This is the method in my Linq to SQL Repository
    public IQueryable GetNewsPosts()
    {
    return from n in _context.NewsPosts
    select new Business.NewsPost(n);
    }

    ///Same method implementation but in my Mock Repository
    public IQueryable GetNewsPosts()
    {
    if (this.newsList == null)
    {
    this.newsList = new List()
    {
    new NewsPost() { Body = "News Post 1", Title = "Test1", CategoryID = Guid.NewGuid() },
    new NewsPost() { Body = "News Post 2", Title = "Test2", CategoryID = Guid.NewGuid() },
    new NewsPost() { Body = "News Post 3", Title = "Test3", CategoryID = Guid.NewGuid() }
    };
    }
    return this.newsList.AsQueryable();
    }

    The error comes when I call everything like so:

    ConcreteRepository.GetNewsPosts().WithId(someValidGuid);

    Again, it only fails when calling the real repository.  It works fine when using the Mock Code. Any help is greatly appreciated!

  • Re: LINQ Issue: no supported translation to SQL

    07-04-2008, 3:34 PM
    Answer
    • Loading...
    • chris.wall
    • Joined on 07-03-2008, 10:17 PM
    • Posts 2
    • Points 0

    Well, changing 

    chris.wall:

    <code>
    ///This is the method in my Linq to SQL Repository
    public IQueryable GetNewsPosts()
    {
    return from n in _context.NewsPosts
    select new Business.NewsPost(n);
    }

    </code>

     

     

    To be

     

    <code>

        return from n in _context.NewsPosts
    select new Business.NewsPost
                {
    ID = n.ID,
    Title = n.Title,
    Body = n.Body
    };
     </code>
     
    It all works as expected now, I'm just not sure why it wouldn't work by passing "n" into the business object's constructor.  Any ideas?  at least I found a way to get it working.. 

     

Page 1 of 1 (2 items)