views:

178

answers:

6

how come this work

public IQueryable<Category> getCategories(int postId)  
{
      subnusMVCRepository<Categories> categories = new subnusMVCRepository<Categories>();
      subnusMVCRepository<Post_Category_Map> postCategoryMap = new  subnusMVCRepository<Post_Category_Map>();

      var query = from c in categories.GetAll()
                  join pcm in postCategoryMap.GetAll() on c.CategoryId equals pcm.CategoryId
                  where pcm.PostId == 1
                  select new Category
                  {
                    Name = c.Name,
                    CategoryId = c.CategoryId
                  };
       return query;
}

but this does not

public IQueryable<Category> getCategories(int postId)  
{
subnusMVCRepository<Categories> categories = new subnusMVCRepository<Categories>();
subnusMVCRepository<Post_Category_Map> postCategoryMap = new subnusMVCRepository<Post_Category_Map>();

    var query = from c in categories.GetAll()
                join pcm in postCategoryMap.GetAll() on c.CategoryId equals pcm.CategoryId
                where pcm.PostId == postId
                select new Category
                {
                    Name = c.Name,
                    CategoryId = c.CategoryId
                };
    return query;
  }
A: 

What errors do you get? is it a compile time error or a runtime error ?

Alexandre Brisebois
A: 

run time error

There is no connection from the object type ConsoleApplication2.Program + <> c__DisplayClass1 to a known managed provider of embedded type.
Subnus
A: 

The problem is not the linq itself, you need to be sure that the context or provider object is able to fetch the data. try testing the

subnusMVCRepository<Categories> categories = new subnusMVCRepository<Categories>();
subnusMVCRepository<Post_Category_Map> postCategoryMap = new subnusMVCRepository<Post_Category_Map>();

objects and see if they are populated or if they behaving as required.

you may want to search the generated code for c__DisplayClass1 and see what you can see there. some times the generated code dose some weird things.

when you step into you code check the locals and the variable values. this may also give you some clues.

Edit : Have you tried to return a List<> collection ? or an Enumerable type?

Edit : What is the real type of the item and query may not be iterable

Alexandre Brisebois
gives the same error
Subnus
i have upload a sample console Application that crash http://www.subnus.net/ConsoleApplication3.zip
Subnus
A: 

the are populated when i call Getll on them but if i can say

 where pcm.PostId == 1

and that works

why does this not work

 where pcm.PostId == postId

the generated code is

{Table(subnusMVC.Categories).Join(Table(subnusMVC.Post_Category_Map), c => c.CategoryId, pcm => pcm.CategoryId, (c, pcm) => new <>f__AnonymousType0`2(c = c, pcm = pcm)).Where(<>h__TransparentIdentifier2 => (<>h__TransparentIdentifier2.pcm.PostId = value(ConsoleApplication2.Program+<>c__DisplayClass3).postId)).Select(<>h__TransparentIdentifier2 => new Category() {Name = <>h__TransparentIdentifier2.c.Name, CategoryId = <>h__TransparentIdentifier2.c.CategoryId})}
Subnus
A: 

mnour this is the Application to test it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using subnusMVC;

namespace ConsoleApplication2
{
  class Program
  {
 static void Main(string[] args)
 {

  var query = getCategories(1);

  foreach (var item in query) <-- error happens here
  {
   Console.WriteLine(item.Name);
  }
  Console.ReadLine();
 } 
 public static IQueryable<Category> getCategories(int Id)
 {
  int postId = Id;
  subnusMVCRepository<Categories> categories = new subnusMVCRepository<Categories>();
  subnusMVCRepository<Post_Category_Map> postCategoryMap = new subnusMVCRepository<Post_Category_Map>();

  var query = from c in categories.GetAll()
     join pcm in postCategoryMap.GetAll()on c.CategoryId equals (pcm.CategoryId)
                    where pcm.PostId == postId
     select new Category
     {
      Name = c.Name,
      CategoryId = c.CategoryId
     };
  return query;
 }
}

}

Subnus
+3  A: 

Hi,

The issue is most likely in the implementation of the query provider.

pcm.PostId == 1

and

pcm.PostId == postId

actually have a big difference. In the expression tree the first is generated as a ConstantExpression which doesnt need to be evaulated.

With the second, the compiler actually generates an inner class here (this is the _DisplayClassX that you see). This class will have a property (will most likely be the same name as your parameter) and the expression tree will create a MemberAccessExpression which points to the auto-generated DisplayClassX. When you query provider comes accross this you need to Compile() the Lambda expression and evaluate the delegate to get the value to use in your query.

Hope this helps.

cosullivan