Linq中子查询的有效方法

时间:2022-01-27 01:13:27

here is my linq code:

这是我的linq代码:

BOOK entity = db.BOOKS
             .Where(s => s.ID == (from p in db.LIBRARY
                                  from b in db.BOOKS
                                  where (p.ID == 123) && (p.idpage == b.idpage)
                                  select b.fields));

My actual oracle code is:

我的实际oracle代码是:

SELECT DISTINCT BOOKS.ID 
FROM LIBRARY,BOOKS 
WHERE LIBRARY.ID = 123 AND LIBRARY.ID = BOOKS.ID

But its showing the error in s.ID that..

但它显示s.ID中的错误..

Delegate 'System.Func Project.Models.BOOKS,int,bool' does not take 1 arguments

Why does this happen? Are there any workarounds?

为什么会这样?有没有解决方法?

4 个解决方案

#1


2  

Your SQL is using a join, so you can do the same thing in LINQ. Either of these approaches will suffice:

您的SQL正在使用连接,因此您可以在LINQ中执行相同的操作。这些方法中的任何一种都足够了:

// join
var query = (from b in db.BOOKS
            join p in db.LIBRARY on b.IdPage equals p.IdPage 
            where p.ID == 123
            select b.Id).Distinct();

// 2 from statements (SelectMany) can also be used as a join
var query = (from b in db.BOOKS
            from p in db.LIBRARY
            where p.ID == 123 && b.IdPage == p.IdPage 
            select b.Id).Distinct();

// fluent syntax
var query = db.BOOKS
              .Where(b => db.LIBRARY.Any(p =>
                  p.ID == 123 && b.IdPage == p.IdPage))
              .Select(b => b.Id)
              .Distinct();

#2


1  

s.ID is comparing to an Enumerable, so you get the error.
At the end of the LINQ query, add a SingleOrDefault().

s.ID与Enumerable进行比较,因此您得到错误。在LINQ查询结束时,添加SingleOrDefault()。

#3


1  

Your subquery returns a sequence of values, not a single values, so you can't compare it to a scalar property like ID. You should use First on the result of the subquery to get the first result (or Single if there should be only one)

您的子查询返回一系列值,而不是单个值,因此您无法将其与ID等标量属性进行比较。您应该在子查询的结果上使用First来获取第一个结果(如果只有一个,则应使用Single)

BOOK entity = db.BOOKS
             .Where(s => s.ID == (from p in db.LIBRARY
                                  from b in db.BOOKS
                                  where (p.ID == 123) && (p.idpage == b.idpage)
                                  select b.fields).First());

#4


0  

You should be able to use the navigation properties on your BOOKS class to do something like this:

您应该能够使用BOOKS类上的导航属性执行以下操作:

var bookIds = db.BOOKS.Where(b => b.LIBRARIES.Any(l => l.ID == 123))
     .Select(b => b.ID)

#1


2  

Your SQL is using a join, so you can do the same thing in LINQ. Either of these approaches will suffice:

您的SQL正在使用连接,因此您可以在LINQ中执行相同的操作。这些方法中的任何一种都足够了:

// join
var query = (from b in db.BOOKS
            join p in db.LIBRARY on b.IdPage equals p.IdPage 
            where p.ID == 123
            select b.Id).Distinct();

// 2 from statements (SelectMany) can also be used as a join
var query = (from b in db.BOOKS
            from p in db.LIBRARY
            where p.ID == 123 && b.IdPage == p.IdPage 
            select b.Id).Distinct();

// fluent syntax
var query = db.BOOKS
              .Where(b => db.LIBRARY.Any(p =>
                  p.ID == 123 && b.IdPage == p.IdPage))
              .Select(b => b.Id)
              .Distinct();

#2


1  

s.ID is comparing to an Enumerable, so you get the error.
At the end of the LINQ query, add a SingleOrDefault().

s.ID与Enumerable进行比较,因此您得到错误。在LINQ查询结束时,添加SingleOrDefault()。

#3


1  

Your subquery returns a sequence of values, not a single values, so you can't compare it to a scalar property like ID. You should use First on the result of the subquery to get the first result (or Single if there should be only one)

您的子查询返回一系列值,而不是单个值,因此您无法将其与ID等标量属性进行比较。您应该在子查询的结果上使用First来获取第一个结果(如果只有一个,则应使用Single)

BOOK entity = db.BOOKS
             .Where(s => s.ID == (from p in db.LIBRARY
                                  from b in db.BOOKS
                                  where (p.ID == 123) && (p.idpage == b.idpage)
                                  select b.fields).First());

#4


0  

You should be able to use the navigation properties on your BOOKS class to do something like this:

您应该能够使用BOOKS类上的导航属性执行以下操作:

var bookIds = db.BOOKS.Where(b => b.LIBRARIES.Any(l => l.ID == 123))
     .Select(b => b.ID)