开发者

LINQ query with specified number of rows in a subquery problem

I'm trying to write the following query in LINQ, but can't seem to get it correct.

select p.*
from Person p
inner join PersoniPhones i ON p.PersonID = i.PersonID
where p开发者_开发百科.PersonID in
(
    SELECT PersonID
    FROM
        (
        SELECT Top 10 PersonID, iPhoneID
        FROM iPhone
        ORDER BY LastPlayedDate DESC
        ) as t
)

I've tried the following, but it doesn't return correctly

        var tenIPhones = from phone in context.PersonIPhones
                         .OrderByDescending(i => i.LastPlayedDate)
                         .Take(10)
                         select new { phone.PersonID, phone.IPHoneID};
        var people = from p in context.Person
                 join t in tenIPhones on p.PersonID equals t.PersonID
                 select p;

Any ideas?


What about

var subsetPhone = iPhones.OrderByDescending(x => x.LastPlayedDate).Take(10).Select(x => new { PersonId = x.PersonId, iPhoneId = x.iPhoneId });
var people = persons.Where(x => subsetPhone.Where(y => y.PersonId == x.Id).Count() > 0);


This works in LinqPad notice IPhoneID and not IPHoneID

var tenIPhones = (from i in PersoniPhones
    .OrderByDescending(i => i.LastPlayedDate)
    .Take(10)
    select new { i.PersonID, i.IPhoneID});

tenIPhones.Dump();  

var people = from p in Persons
         join t in tenIPhones on p.PersonID equals t.PersonID
         select p;

people.Dump();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜