And ... it's back.  OK, I think I've narrowed it down to the exact
problem.  Before I broke everything down into an example I had a
helper func that did the object projection (so I wouldn't have to
repeat the "new User { ... }" code wherever I did a projection).  This
is when things go boom.

static void Main()
{
  var userRepo = new UserRepository(new TestDb());
  var abe = userRepo.GetAll().Where(u => u.Name == "Abe").SingleOrDefault();
  Console.WriteLine("{0}", abe.Id);
}

Works:

    public class UserRepository
    {
        TestDb _db;

        public UserRepository(TestDb db)
        {
            _db = db;
        }

        public IQueryable<User> GetAll()
        {
            return from u in _db.Users
                select new User
                {
                    Email = u.Email,
                    Id = u.UserID,
                    Name = u.Name
                };
        }
    }

Doesn't work:

    public class UserRepository
    {
        TestDb _db;

        public UserRepository(TestDb db)
        {
            _db = db;
        }

        public IQueryable<User> GetAll()
        {
            return from u in _db.Users select LoadUser(u);
        }

        User LoadUser(Users record)
        {
            return new User
            {
                Email = record.Email,
                Id = record.UserID,
                Name = record.Name
            };
        }
    }

Since I have a work-around this isn't nearly a priority as when I
thought there was no way to project types.  So no worries if this
can't get attention.  Apologies for all the going back and forth.

-Abe

On Thu, Jul 1, 2010 at 1:10 AM, Abe Gillespie <[email protected]> wrote:
> OK, totally weird.  After not looking at it for a few days and then
> endeavoring to put a complete, compilable example together for you,
> I'm now not having the issue.
>
> Sorry for the troubles.  Enjoy your vacation!
>
> -Abe
>
> On Thu, Jun 24, 2010 at 3:54 PM, Jonathan Pryor <[email protected]> wrote:
>> On Thu, 2010-06-24 at 14:53 -0400, Abe Gillespie wrote:
>>> I'll be the first to admit I'm no Linq genius (hardly), but I'm
>>> thinking the deferred execution of the whole thing ultimately has the
>>> "new" op showing up in the where clause.  If I have this right it's
>>> essentially doing the following query:
>>>
>>> var me = from u in _db.Users
>>>               where u.Status == "active" && new User(u).Name == "Abe"
>>>               select new User(u);
>>>
>>> Don't worry, I'm using DI to send the database context into the
>>> constructor.  I just threw the example together ... not even sure if
>>> it would compile.
>>
>> Trying to get something that (1) compiles, and (2) actually hits the
>> error you're encountering would be extremely helpful so that we're (1)
>> on the same page wrt where the bug lies, and (2) can sanely discuss
>> workarounds. :-)
>>
>> To the best of my knowledge, this:
>>
>>        userRepo.GetAll().Where(u => u.Name == "abe")
>>
>> should NOT result in creation of anything within the .Where()
>> expression.
>>
>> For good measure, in order to better mimic what I *think* you're
>> attempting to do, I wrote this:
>>
>>        class E {
>>            public int ID;
>>            public string Name;
>>        }
>>
>>        [Test]
>>        public void TESTME()
>>        {
>>            Northwind db = CreateDB();
>>            var nancy = GetEmployees (db).Where (e =>
>>                    e.Name == "Nancy Davolio")
>>                .SingleOrDefault ();
>>            Console.WriteLine (nancy.Name);
>>        }
>>
>>        static IQueryable<E> GetEmployees(Northwind db)
>>        {
>>            return from e in db.Employees
>>                where e.Region == "WA"
>>                select new E {
>>                    ID    = (int) e.EmployeeID,
>>                    Name  = e.FirstName + " " + e.LastName,
>>                };
>>        }
>>
>> I believe this is vaguely close to what you did, in that we're taking
>> the database types (Employee), selecting them into a user type (E), and
>> then further querying that to select a specific entry ("nancy").
>>
>> This works as is, no exceptions generated.
>>
>> I would appreciate it if you could provide a compiling, valid, FAILING
>> sample UNLESS the compiling+failing sample is close to my previous
>> sample which has an explicit 'new' within the 'where'.
>>
>> Thanks,
>>
>>  - Jon
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "DbLinq" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to 
>> [email protected].
>> For more options, visit this group at 
>> http://groups.google.com/group/dblinq?hl=en.
>>
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"DbLinq" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/dblinq?hl=en.

Reply via email to