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();
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
}
Anyway, First<T>()
also have an overload First<T>(Predicate<T>)
, ie:
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();
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
:)
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.