Reputation: 1941
I have a problem with translation my function when i try to select data from table. So, now i have about 130000 rows with "words" and they were written on my language- russian:
привет, пока, москва.
But i need to translate this words on english and find them in database with their english transcription:
привет = privet, пока = poka, москва = moskva.
I just make Replace ('п' -> 'p', 'т' -> 't').
But when i'm tring to execute extension method on LINQ table property, i have an exception, and i know it. So, how could i use function on property in
.Where(t=> FunctionReplaceName(t.Name)
) ?
Thx.
public static string Translate(this string stringToTranslate)
{
string[] kirArray = { "А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж", "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ы", "Ь", "Э", "Ю", "Я", ",", ".", " ", "(", ")" };
string[] latArray = { "A", "B", "V", "G", "D", "E", "JO", "ZH", "Z", "I", "IJ", "K", "L", "M", "N", "O", "P", "R", "S", "T", "U", "F", "H", "TZ", "CH", "SH", "SH", "", "JY", "", "JE", "JU", "JA", "", "", "-", "", "" };
stringToTranslate = stringToTranslate.ToUpper();
for (int i = 0; i < kirArray.Length; i++)
if (stringToTranslate.IndexOf(kirArray[i]) != -1)
stringToTranslate = stringToTranslate.Replace(kirArray[i], latArray[i]);
return stringToTranslate.ToLower();
}
Upvotes: 0
Views: 358
Reputation: 85966
LINQ-to-SQL doesn't know how to translate FunctionReplaceName
to SQL, so it's giving you an error.
Try replacing it with this:
.ToList().Where(t=> FunctionReplaceName(t.Name));
Note that this will pull everything into memory from the database, so apply your other .Where
clauses before this.
Upvotes: 0
Reputation: 77546
First, your where predicate is invalid:
.Where(t=> FunctionReplaceName(t.Name))
That is not a valid where clause. It needs to return a bool. Do you actually have something like this?
.Where(t=> FunctionReplaceName(t.Name) == inputName)
If so, you are doing it backwards -- you are (syntactically) trying to convert the value in the DB using your function. Instead you should do as you were trying, "to translate this words on english and find them in database with their english transcription."
You should have:
.Where(t=> t.Name == FunctionReplaceName(inputName))
I think that should work, but possibly sql translation may still barf on your function call, in which case you should call your function on the value (inputName
) and store it in a string variable before calling Where
.
Upvotes: 1