linq to entity не распознает метод - PullRequest
0 голосов
/ 08 мая 2020

Мне нужно вызвать метод FormatCourseTitle () из запроса linq, но я получаю сообщение об ошибке «Linq to Entity не распознает метод FormatCourseTitle ...» Как решить эту проблему?

 public ActionResult Index()
{
    var searchResults = (from a in db.Courses
                    join b in db.Summary on
                    new { subject = a.subject, catalog = a.catalog, coursetitle = FormatCourseTitle(a.coursetitle) } equals
                                  new { subject = b.Subject, catalog = b.Catalogno, coursetitle = b.CourseTitle } into ab
                                 from b in ab.DefaultIfEmpty()
                                 where a.Active == true 
                                 select new JoinModel
                                 {
                                     Courses = a,
                                     Summary2020 = b
                                 } ).ToList();
    return View(searResults);

}
 public class JoinModel
{
    [Key]
    public int Id { get; set; }

    public Courselist Courses { get; set; }
    public Contentsummary Summary { get; set; }

}

 public string FormatCourseTitle(string courseTitle)
    {
        //find if last three characters are " or"
        string[] words = courseTitle.Trim().ToLower().Split(' ');
        int j = words.Count();
        string tempStr = string.Empty;              
            if (words[j] == "or")
            {
                tempStr = courseTitle.Substring(0, courseTitle.Length - 3);
            }
            else
            {
                tempStr = courseTitle;
            }


            return tempStr;
    }

Ответы [ 2 ]

1 голос
/ 08 мая 2020

Вы должны использовать метод расширения как

public static class StringHelper
    {

        public static string FormatCourseTitle(this string courseTitle)
        {
            //find if last three characters are " or"
            string[] words = courseTitle.Trim().ToLower().Split(' ');
            int j = words.Count();
            string tempStr = string.Empty;
            if (words[j] == "or")
            {
                tempStr = courseTitle.Substring(0, courseTitle.Length - 3);
            }
            else
            {
                tempStr = courseTitle;
            }


            return tempStr;
        }
    }

Изменить запрос на

var searchResults = (from a in db.Courses
                    join b in db.Summary on
                    new { subject = a.subject, catalog = a.catalog, coursetitle = a.coursetitle.FormatCourseTitle() } equals
                                  new { subject = b.Subject, catalog = b.Catalogno, coursetitle = b.CourseTitle } into ab
                                 from b in ab.DefaultIfEmpty()
                                 where a.Active == true 
                                 select new JoinModel
                                 {
                                     Courses = a,
                                     Summary2020 = b
                                 } ).ToList();
0 голосов
/ 08 мая 2020

Я нашел способ добавить условие в запрос linq для решения этой проблемы.

var searchResults = (from a in db.Courses
                join b in db.Summary on
                new { subject = a.subject, catalog = a.catalog, coursetitle = a.coursetitle.Trim().EndsWith(" or")?a.coursetitle.Substring(0,a.coursetitle.Length-3):a.coursetitle } equals
                              new { subject = b.Subject, catalog = b.Catalogno, coursetitle = b.CourseTitle } into ab
                             from b in ab.DefaultIfEmpty()
                             where a.Active == true 
                             select new JoinModel
                             {
                                 Courses = a,
                                 Summary2020 = b
                             } ).ToList();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...