0

Any ideas as to why this query compiles, but then throws this runtime error:

Argument expression is not valid

I know I could change my db model, but in this case it's not possible. Any ideas how to get something like this working? Not even sure what this would be called. Thanks.

DBContext db = new DBContext();
var books = (from b in db.BOOKS
             select new
             {
                 b.ID,
                 b.NAME,
                 AuthorName = db.PEOPLEs.Where(p=>p.ID==b.AUTHOR).First().USER_ID,
             }).ToList();
VulgarBinary
  • 3,520
  • 4
  • 20
  • 54
brad oyler
  • 3,669
  • 1
  • 20
  • 22

4 Answers4

1

I've found I have the best luck with complex inner queries by using let expressions. This does a subselect and allows you greater flexibility to bind an element from a subselect. HOWEVER, notice that I am only doing a First() on the author assignment in the anonymous object. This is because if you do a First().PropertyName and First yields a null value it will blow up.

Good luck and double check syntax. I don't have your full object set so I cannot generate a fully working demo, however, this was tested with an object tree that I have on one of my own projects.

var books = (
        from b in db.BOOKs
        let author = (from a in db.PEOPLEs
                      where b.AUTHOR == a.ID
                      select a)
        select new
        {
            ID = b.ID,
            NAME = b.Name,
            Author = author.First()
        }
    ).ToList();    

foreach(var x in books)
{
    string AuthorName = x.Author.USER_ID;
    //Do other stuff
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
VulgarBinary
  • 3,520
  • 4
  • 20
  • 54
0

Anyway, First<T>() also have an overload First<T>(Predicate<T>), i.e.:

AuthorName = db.PEOPLEs.First(p=>p.ID==b.AUTHOR).USER_ID

You can use LINQ in methods styles:

var books = (from b in db.BOOKS
             let author = db.PEOPLEs.Where(p => p.ID == b.AUTHOR).First()
             select new
             {
                 b.ID,
                 b.NAME,
                 AuthorName = author.USER_ID,
             }).ToList();
abatishchev
  • 98,240
  • 88
  • 296
  • 433
0

If you have a look on your code, you have

DBContext db = new DBContext();
var books = (from b in db.BOOKS
select new
{
    b.ID,
    b.NAME,
    AuthorName = db.PEOPLEs.Where(p=>p.ID==b.AUTHOR).First().USER_ID, <<-- Here coma
}).ToList();

It is expecting another parameter there, just remove it and it sould pass

:)

VulgarBinary
  • 3,520
  • 4
  • 20
  • 54
cpoDesign
  • 8,953
  • 13
  • 62
  • 106
0

So, the final solution was:

var books = (
    from b in db.BOOKs
    let author = (from a in db.PEOPLEs
                    where b.AUTHOR == a.ID
                    select a)
    select new
    {
        ID = b.ID,
        NAME = b.Name,
        Author = author.First().USER_ID
    }
).ToList();

Thanks!

brad oyler
  • 3,669
  • 1
  • 20
  • 22