“参数表达式无效” EF4 Linq 子选择查询出错

发布于 2024-10-20 20:59:39 字数 443 浏览 2 评论 0原文

关于为什么此查询编译但随后抛出此运行时错误的任何想法:

参数表达式无效

我知道我可以更改我的数据库模型,但在这种情况下这是不可能的。 有什么想法如何让这样的事情发挥作用吗?甚至不确定这会被称为什么。谢谢。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只是一片海 2024-10-27 20:59:39

我发现通过使用 let 表达式,我在复杂的内部查询方面运气最好。这会执行子选择,并允许您更灵活地绑定子选择中的元素。但是,请注意,我仅对匿名对象中的作者分配执行 First() 。这是因为如果你执行 First().PropertyName 并且 First 产生一个 null 值,它就会爆炸。

祝你好运并仔细检查语法。我没有完整的对象集,因此我无法生成完全工作的演示,但是,这是使用我自己的项目之一的对象树进行测试的。

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
}

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
}
我不是你的备胎 2024-10-27 20:59:39

如果你看一下你的代码,你会

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

发现它需要另一个参数,只需删除它,它就会通过

:)

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

:)

情绪失控 2024-10-27 20:59:39

所以,最终的解决方案是:

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

谢谢!

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!

巷雨优美回忆 2024-10-27 20:59:39

无论如何,First()也有一个重载 First(Predicate),即:

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

您可以在方法样式中使用 LINQ:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文