I have a LINQ query which searches for a string (using a regex) in multiple fields. I want to sort the results based on in which field the text was found.
Currently I have this:
var results = from i in passwordData.Tables["PasswordValue"].AsEnumerable()
where r.IsMatch(i.Field<String>("Key").Replace(" ","")) ||
r.IsMatch(i.Field<String>("Username").Replace(" ","")) ||
r.IsMatch(i.Field<String>("Other").Replace(" ",""))
orderby i.Field<String>("Key"),
i.Field<String>("Other"),
i.Field<String>("Username")
select i;
I want matches found in Key first, then matches found in Other, and then matches found in Username. If possible, matches which match both Key and Other should go before matches that match only Key.
The code I currently have sorts based on Key first, so if a match is found on Other but Key starts with an A, it will be sorted before a match found on Key where Key starts with a Z.
Thanks in advance, it isn't a hard question I think but I just can't figure out how to do this as I'm new to LINQ.