0

I have a class/entity over a project.

Entity example:

public class Project
{
    public int Id { get; set; }
    public string? ProjectId { get; set; }
    public string Name { get; set; }
}

In Blazor file I have this code below. projList get data from my repository, filterList = projList works

@code {
    private IEnumerable<Project> projList { get; set; } = new List<Project>();
    private List<Project> filterList { get; set; } = new List<Project>();

    public string projFilter { get; set; } = string.Empty;

    ..
    
    private async Task ApplyProjFilter()
    {
        ..
        filterList = projList.Where(p => p.ProjectId.Contains(projFilter)).ToList();
        ..
    }

I get an error on the above instruction with .Contains. I can use

(p => p.ProjectId == projFilter).ToList() 

but I want to filter my list on the run when the user writes a filter in the textbox and show the projects that contain that input string.

.Contains works if I use it in my repository when I read data from the server but I will not use it on the run because it will cause a lot of questions to SQL-server.

Any idéa what is wrong?

I'm using the latest netcore and efcore and Visual Studio 2019.

Akif
  • 7,098
  • 7
  • 27
  • 53
  • So you want to show any projects where the `ProjectId` string contains the substring `projFilter`? Or starts with? – Kirk Woll Feb 10 '21 at 16:12

1 Answers1

0

I post a code snipped based on your question, be free to change it to match your requirements:

<input type="text" @oninput="@( (e) => ApplyProjFilter(e) )">
<li>
@foreach(var p in filterList)
{
    <li>@p.Name</li>
}
</li>

@code {
    public class Project
    {
        public string Name { get; set; }
    }

    private IEnumerable<Project> projList { get; set; } 
    private List<Project> filterList { get; set; } 
    public string projFilter { get; set; } = string.Empty;

    protected override void OnInitialized()
    {
        projList = new []
        {
            new Project{ Name = "pA"},
            new Project{ Name = "pB"} //, ...
        }.ToList();
        filterList = projList.ToList();
    }

    private async Task ApplyProjFilter(ChangeEventArgs e)
    {
        projFilter = e.Value.ToString();
        filterList = projList.Where(p => p.Name.Contains(projFilter)).ToList();
    }
}

Check it out running at: https://blazorrepl.com/repl/QPOGluvA27eghIPO51

Debounce:

Reading "it will cause a lot of questions to SQL-server" I think, maybe, you are looking for debounce

In some cases it is not a good idea to get all remote data to the client side (if you have more than 300 o 400 records this request can kill performance)

Take a look to this answer to learn about debounce on blazor: https://stackoverflow.com/a/57545940/842935

dani herrera
  • 48,760
  • 8
  • 117
  • 177
  • Thanks for your answer. But I still got the same error : System.NullReferenceException: 'Object reference not set to an instance of an object.' DataAccess.Data.Project.ProjectId.get returned null. – Tobias Gustafsson Feb 10 '21 at 16:48
  • Could it be that I have an public virtual ICollection ProjDatas { get; set; } ? in my Class Project? – Tobias Gustafsson Feb 10 '21 at 16:54
  • In your code `ProjectId` is nullable. Before compare check it is not null: `...ProjectId != null && ...` – dani herrera Feb 10 '21 at 16:56