I would like to orderBy list by ShortName and then by phone number. Firstly show records with phoneNumber then without.
I tried:
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";
}
but in result i get:
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.'
Has anyone idea how to workaround this?
Taking into account the suggestions from the comments, I tried to think about it and worked out a working but propably not the most effective solution for the problem. Below code stands for .OrderByDescening
firstly by first letter of short name and then place at the end items which leading contact has no phone number (knowing that no phone number can also be entered as "---"
.
var x = q.OrderByDescending(e => e.ShortName.Substring(0, 1))
.ThenBy(c => (c.Contacts.FirstOrDefault(p => p.Id == c.LeadingContact).PhoneNumber ?? "---").Equals("---") ? 1 : 0);
If u has knowledge how to optimize or write it better, I would appreciate your suggestions