2

I'm trying to create the following query in C# with EntityFrameWork:

SELECT * FROM Companies WHERE Name LIKE '%(searchValue)%';

This is the table sample: data

Company class:

public class Company {     
    [Key]     
    public int ID { get; set; }     
    public string? Name { get; set; }     
    public int IndustrySegment { get; set; }     
    public string? Departments { get; set; }     
    public int MWCustomer { get; set; }     
    public int ActiveLicense { get; set; }     
    public string? Employees { get; set; }     
    public string? Selections { get; set; }

}

This is how C# code is set:

var context = new FILTER_SYSTEM_CONTEXT();
var connectionCompanies = new DAL_SYSTEM<Company>(context);
var searchByName = connectionCompanies
    .Where(a => (a.Name ?? "").Contains(searchValue))
    .ToList();`

The idea is to search for all companies, returning a List, in this table that may contain the 'searchValue', typed by the user.

But this code keeps returning the following error:

`Data is Null. This method or property cannot be called on Null values.

Value cannot be null. (Parameter 'source')`

Expected: Not facing any "NULL" returns anymore.

What did I try: The fixing I found so far, was to completly fill all those "NULL" values in Companies DB Table. But all that values are suppoed to be optional, so they may be NULL some day.

5
  • How are the properties in your Company class defined in c# (are they nullable)? Commented Aug 15, 2024 at 17:27
  • 1
    Yes, you're right. Just a misspelled word here. Now it's correct Commented Aug 15, 2024 at 17:28
  • public class Company { [Key] public int ID { get; set; } public string? Name { get; set; } public int IndustrySegment { get; set; } public string? Departments { get; set; } public int MWCustomer { get; set; } public int ActiveLicense { get; set; } public string? Employees { get; set; } public string? Selections { get; set; } Commented Aug 15, 2024 at 17:29
  • 1
    Shouldn't MWCustomer and ActiveLicense be int? -- at least, according to the data sample in your pic, they are null. Also, string? is redundant because string is nullable by default. Commented Aug 15, 2024 at 17:32
  • 1
    you right, issue fixed with both MWCustomer and ActiveLicense set int?. Thank you!! Commented Aug 15, 2024 at 17:47

1 Answer 1

1

Try changing your class definition to:

public class Company {     
    [Key]     
    public int ID { get; set; }     
    public string Name { get; set; }     
    public int IndustrySegment { get; set; }     
    public string Departments { get; set; }     
    public int? MWCustomer { get; set; }     
    public int? ActiveLicense { get; set; }     
    public string Employees { get; set; }     
    public string Selections { get; set; }

}

In your data sample, both MWCustomer and ActiveLicense are null, but your class definition has them as non-nullable int. Changing them to int? makes them nullable.

Also, string? is redundant because that data type is nullable by default. So you can just use string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.