在 Linq orderBy 中使用外部方法

Use external method in Linq orderBy

我想先按短名称排序,然后再按 phone 编号排序。首先显示带有 phone 编号的记录,然后显示没有

的记录

我试过了:

var x = q.OrderByDescending(e => e.ShortName).ThenBy(c => SqlFunctions.IsNumeric(Order(c)));

private string Order(Contractor c)
{
    var phoneNumber = c.Contacts.AsEnumerable().FirstOrDefault(p => p.Id == c.LeadingContact)?.PhoneNumber ?? "";
    if (string.IsNullOrEmpty(phoneNumber) || phoneNumber.Equals("---"))
        return "a";
    return "1";
}

但结果我得到:

System.NotSupportedException: 'LINQ to Entities does not recognize the method 'System.String Order(DOCR.Domain.Entities.CRM.Contractor)' method, and this method cannot be translated into a store expression.'

有人知道如何解决这个问题吗?

考虑到评论中的建议,我尝试考虑并制定了一个可行但可能不是最有效的问题解决方案。下面代码代表.OrderByDescening先用简称首字母表示,然后把前导联系人没有phone号的项放在末尾(要知道没有phone号也可以输入为"---".

var x = q.OrderByDescending(e => e.ShortName.Substring(0, 1))
.ThenBy(c => (c.Contacts.FirstOrDefault(p => p.Id == c.LeadingContact).PhoneNumber ?? "---").Equals("---") ? 1 : 0);

如果你知道如何优化或更好地编写它,我将不胜感激你的建议