“Argument expression is not valid” error with EF4 Linq sub select query

浪尽此生 提交于 2019-12-11 17:17:06

问题


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();

回答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
}



回答2:


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

:)




回答3:


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!




回答4:


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();


来源:https://stackoverflow.com/questions/5224976/argument-expression-is-not-valid-error-with-ef4-linq-sub-select-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!