gpt4 book ai didi

linq-to-sql - linq to sql 是否有 with ties 选项?

转载 作者:行者123 更新时间:2023-12-02 00:44:22 25 4
gpt4 key购买 nike

我正在尝试将以下查询移动到 Linq-to-sql,这可能吗?

select * from (
Select top (@Percent) percent with ties *
from(
Select distinct
LoanNumber as LoanNo
From CHE
Left Join RecordingInfo as Rec
On CHE.LoanNumber = Rec.LoanNo
Where Channel = 'LINX'
and CHE.Doc in ('MTG','MOD')
and Rec.LoanNo is null
and LoanNumber >= '@LoanNo'
) A
order by LoanNo @Order
) B
order by LoanNo

无论如何,我还没有看到与 linq 中的关系有关。

最佳答案

我认为这样的东西对你有用。

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

我只用 IEnumerable 测试过它,所以我不确定它是否能与 IQueryable 一起正常工作。我还在调用 TopPercentWithTies() 之前对列表进行了排序。

这是我用来测试它的代码。

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

希望它对您有所帮助,或者至少让您朝着正确的方向前进。您可能想要调整计算 numberToTake 的逻辑。

关于linq-to-sql - linq to sql 是否有 with ties 选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1342848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com