2

My problem as it is right now. That is, it does not go down to the other table which is courseCategory which is put together with CourseData.

I would therefore like to get hold of courses that have been set up on the single category value.

Let's say that Course did not find anything on any of the others while addressing my category content.

As you can see in the picture here, for example, I just have several right now, but when each course adds a category. and it is not found on any of the others. Well then, search for the single category should take over.

My table from category My table from category

My database overview My database overview

So my problem to make it clear is that it basically does not address the words from SearchWord. As long as there is one or more words that fit, it must address it.

public List<CourseData> GetCourseList(string text)//Undervisning
    {
        List<CourseData> list = new List<CourseData>();
        var dataContent = _dbContext.CourseDataModul
            .Where(r => r.Title.Contains(text) 
                        || r.Deck.Contains(text) 
                        || r.CourseDataContent.FirstOrDefault(x => x.Title.Contains(text)).Title.Contains(text) 
                        || r.CourseDataContent.FirstOrDefault(x => x.Deck.Contains(text)).Deck.Contains(text)
                        || r.CourseDataContent.FirstOrDefault(x => x.ContentValue.Contains(text)).ContentValue.Contains(text))
            .ToList();
        
        foreach (var item in dataContent)
        {
            list = _dbContext.CourseData
                .Where(r => r.Id == item.CourseDataId && r.OpenNow || r.Title.Contains(text) || r.Deck.Contains(text) ||
                            r.CourseCategori.SearchWord
                                .Split(new [] {",", ", ", " "}, StringSplitOptions.None) //online undervisning,undervisning,computer undervisning
                                .Contains(text)).ToList();//undervisning
        }
        return list;
    }

You can see in the code by split that I have commented on some code which should tell what it looks like in the database and that it should for example just be based on "undervisning" only. As I said, it is welcome to address more.

I have looked at these here link:

How can i check and split strings which contains comma,backslash,and(&),hyphen

c# split string only if delimiter found

Best way to check for string in comma-delimited list with .NET?

Update.

After I have updated my code and done like this. Then there will be an error which can be seen here.

public List<CourseData> GetCourseList(string text)//Undervisning
    {
        List<CourseData> list = new List<CourseData>();
        var dataContent = _dbContext.CourseDataModul
            .Where(r => r.Title.Contains(text) 
                        || r.Deck.Contains(text) 
                        || r.CourseDataContent.Any(x => x.Title.Contains(text)) 
                        || r.CourseDataContent.Any(x => x.Deck.Contains(text))
                        || r.CourseDataContent.Any(x => x.ContentValue.Contains(text)))
            .ToList();
        if (dataContent.Count > 0)
        {
            foreach (var item in dataContent)
            {
                list = _dbContext.CourseData
                    .Where(r => r.Id == item.CourseDataId && 
                                r.OpenNow || 
                                r.Title.Contains(text) ||
                                r.Deck.Contains(text) ||
                                r.CourseCategori.SearchWord
                                    .Split(new[] {",", ", ", "/"},
                                        StringSplitOptions
                                            .RemoveEmptyEntries) //online undervisning,undervisning,computer undervisning
                                    .Any(x => x.Contains(text))).ToList(); //undervisning
            }
        }
        else
        {
            list = _dbContext.CourseData.Where(r => r.OpenNow &&
                                r.Title.Contains(text) ||
                                r.Deck.Contains(text) ||
                                r.Text.Contains(text) ||
                                r.CourseCategori.SearchWord
                                    .Split(new[] {",", ", ", "/"},
                                        StringSplitOptions
                                            .RemoveEmptyEntries) //online undervisning,undervisning,computer undervisning
                                    .Any(x => x.Contains(text))).ToList();
        }

        return list;
    }

Error code here:

The LINQ expression 'DbSet .Join( outer: DbSet, inner: c => EF.Property<Nullable>(c, "CourseCategoriId"), outerKeySelector: c0 => EF.Property<Nullable>(c0, "Id"), innerKeySelector: (o, i) => new TransparentIdentifier<CourseData, CourseCategori>( Outer = o, Inner = i )) .Where(c => c.Outer.OpenNow && c.Outer.Title.Contains(__text_0) || c.Outer.Deck.Contains(__text_0) || c.Outer.Text.Contains(__text_0) || c.Inner.SearchWord.Split( separator: string[] { ",", ", ", "/", }, options: RemoveEmptyEntries) .Any(x => x.Contains(__text_0)))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

Brad Barber
  • 2,953
  • 1
  • 19
  • 18
J. Petersen
  • 47
  • 1
  • 6
  • 2
    Please review [MCVE] guidance on posting code and [edit] question with minimal code that also includes all data necessary to reproduce the problem. Also make sure code does not scream "NRE here" - like accessing results of `FirstOrDefault` without `?`/checking for null. (Side note `r.CourseDataContent.FirstOrDefault(x => x.Title.Contains(text)).Title.Contains(text)` is very confusing - maybe you were looking for `Any` instaead of `FirstOrDefault`?) – Alexei Levenkov Feb 17 '21 at 02:32
  • EF does not understand `Split`. Are you not getting an error about that? – CodingYoshi Feb 17 '21 at 02:59
  • Yes I get an error after I update in the code compared to previously stated. @CodingYoshi – J. Petersen Feb 17 '21 at 13:15
  • Is that how you think as I show in the code after updater? @AlexeiLevenkov – J. Petersen Feb 17 '21 at 13:16
  • Remove `Split` from the query because EF does not know how to convert it to a SQL query. Bring all the records to the c# side and then you can do a `Split` on them. If there are too many records to bring then either write a stored procedure or a custom EF function that can do the `Split` – CodingYoshi Feb 17 '21 at 18:04

0 Answers0