Does linq to sql have a with ties option?

后端 未结 1 778
逝去的感伤
逝去的感伤 2021-01-14 11:03

I\'m trying to move the following query to Linq-to-sql, is it possible?

select * from (
Select top (@Percent) percent with ties *
from(
    Select distinct
          


        
相关标签:
1条回答
  • 2021-01-14 11:16

    I think something like this will work for you.

    public static IQueryable<T> TopPercentWithTies<T, TKey>(this IOrderedQueryable<T> query, Expression<Func<T, TKey>> groupByExpression, double percent)
    {
        var groupedQuery = query.GroupBy(groupByExpression);
        int numberToTake = groupedQuery.Count() * percent / 100;
        return groupedQuery.Take(numberToTake).SelectMany(t => t);
    }
    

    I only tested it with IEnumerable, so I don't know for sure that it'll work properly with IQueryable. I also sorted the list before calling TopPercentWithTies().

    Here's the code I used to test it.

    int percent = 50;
    var people = new []
    {
        new { Age = 99, Name = "Adam" },
        new { Age = 99, Name = "Andrew" },
        new { Age = 89, Name = "Bob" },
        new { Age = 50, Name = "Cecil" },
        new { Age = 50, Name = "Doug" },
        new { Age = 50, Name = "Everett" },
        new { Age = 35, Name = "Frank" },
        new { Age = 25, Name = "Greg" },
        new { Age = 15, Name = "Hank" }
    };
    var sortedPeople = people.AsQueryable().OrderByDescending(person => person.Age);
    var results = sortedPeople.TopPercentWithTies(person => person.Age, percent);
    foreach (var person in results)
        Console.WriteLine(person);
    

    Hope it helps or at least gets you in the right direction. You may want to tweak the logic for calculating numberToTake.

    0 讨论(0)
提交回复
热议问题