0

SQL Server查询中这个Linq查询的等价物是什么?

IEnumerable<Profile> profiles = am.Profile.Where(a => articles.Select(b => b.ProfileId).Distinct().Contains(a.ProfileID));

哪里articles是:

IEnumerable<Article> articles = (from a in am.Article
                                 orderby a.AddedDate descending
                                 select a).ToList();
4

3 回答 3

3

使用EXISTS

SELECT * FROM Profile P
WHERE EXISTS(
    SELECT 1 FROM Article A
    WHERE A.ProfileId = P.ProfileId
)

除此之外,您的 linq 查询效率低下。为什么Distinct以前用Contains?在使用Contains.

这更具可读性和效率:

IEnumerable<Profile> profiles = am.Profile
    .Where(p => articles.Any(a => a.ProfileID == p.ProfileID));
于 2013-08-13T15:48:48.183 回答
1
SELECT * FROM Profile
WHERE
EXISTS(SELECT DISTINCT ProfileId FROM Article WHERE ProfileId in (SELECT ProfileID FROM  Profile)) 
于 2013-08-13T15:21:01.450 回答
0
Select p.*
From [Profile] p
Where p.profileID In (
    Select Distinct ProfileId
    From [Article]
)
于 2013-08-13T15:28:40.287 回答