Query predicate to search related record if it exists

Dear,

(using Syncfusion.EJ2.AspNet.Core version 18.4.0.33)

I have a class "Product" that has a nullable "ParentProduct".

When searching in a datagrid (using a UrlAdapter datamanager) for text, I want to generate a query that looks for this text in the name of the products, and also - if it exists - in the name of the parent product.

I have mode something like

var predicate = new ej.data.Predicate('Name', 'contains', textToSearch);
predicate = predicate.or('ParentProduct.Name', 'contains', textToSearch);

However, this results in a SQL query that contains the following part:

WHEN [product].[ParentProductId] IS NULL OR ([product].[ParentProductId] IS NOT NULL AND (CHARINDEX(N'mysearchtext', COALESCE([product.ParentProductId].[Name], N'Blanks')) > 0))

the first part "[product].[ParentProductId] IS NULL" causes it to return ALL products without a parentProduct; that is of course not what I want...

How should I achieve what I need?

Thanks,
Koen


9 Replies

SM Shalini Maragathavel Syncfusion Team January 22, 2021 12:28 PM UTC

Hi Koen, 

Thanks for contacting Syncfusion support. 

Before we proceed with your requirement we need the following details, 

1. Share your complete Grid rendering code. 

2. Share the dataSource Structure that you have bounded to the Grid. 

3. Is ‘ParentProduct’ is a foreignKey column in the Grid? 

4. Do you need perform searching operation for the foreignKey value of the Grid? 
 
5. Share the video demonstration of your requirement. 

Regards, 
Shalini M. 



KO Koen replied to Shalini Maragathavel January 26, 2021 07:54 PM UTC

Hi Koen, 

Thanks for contacting Syncfusion support. 

Before we proceed with your requirement we need the following details, 

1. Share your complete Grid rendering code. 

2. Share the dataSource Structure that you have bounded to the Grid. 

3. Is ‘ParentProduct’ is a foreignKey column in the Grid? 

4. Do you need perform searching operation for the foreignKey value of the Grid? 
 
5. Share the video demonstration of your requirement. 

Regards, 
Shalini M. 


Dear,

Bear with me, I'm in the process of creating a condensed version of this problems. I cannot share the real project since it's too big and too complex.
I hope to send it in the coming days...

Thanks,
Koen


SM Shalini Maragathavel Syncfusion Team January 27, 2021 02:23 PM UTC

Hi Koen,  

Thanks for the update. 

We will wait to hear from you. 

Regards, 
Shalini M. 
 



KO Koen replied to Shalini Maragathavel January 28, 2021 02:57 PM UTC

Hi Koen, 

Thanks for contacting Syncfusion support. 

Before we proceed with your requirement we need the following details, 

1. Share your complete Grid rendering code. 

2. Share the dataSource Structure that you have bounded to the Grid. 

3. Is ‘ParentProduct’ is a foreignKey column in the Grid? 

4. Do you need perform searching operation for the foreignKey value of the Grid? 
 
5. Share the video demonstration of your requirement. 

Regards, 
Shalini M. 


Dear,
I have made a demo solution (download here
I must admit I have made a mistake by saying I was using a datagrid. In fact, I am having this issue with a dropdown.

In the dropdown, I show a customer, and if that customer has a parent customer, it is also shown:



When searching for "Gio" I want it to show up two results: "Giovanni Rovelli" and "Anabela Domingues (child of Giovanni Rovelli)".
However it returns "Anabela Domingues (child of Giovanni Rovelli)" AND all customers without a parent customer...




This is the filtering function:

function onfilteringcustomercombobox(e) {
        var CBObj = document.getElementById("customercombobox").ej2_instances[0];

        var words = e.text.split(" ");

        if (words.length >= 1) {

            var queryPredicate = new ej.data.Predicate('ContactName', 'equal', 'xxx');

            for (i = 0; i < words.length; i++) {
                var word = words[i];
                if (word !== '') {
                    queryPredicate = queryPredicate.or('ContactName', 'contains', word);

                    queryPredicate = queryPredicate.or('ParentCustomer.ContactName', 'contains', word);
                }
            }
            var query = new ej.data.Query().where(queryPredicate);
            e.updateData(CBObj.dataSource, query);
        }

    };


This is the resulting query:

      SELECT [t].[CustomerID], [t].[Address], [t].[City], [t].[CompanyName], [t].[ContactName], [t].[ContactTitle], [t].[ParentCustomerID], [Customer.ParentCustomer].[CustomerID], [Customer.ParentCustomer].[Address], [Customer.ParentCustomer].[City], [Customer.ParentCustomer].[CompanyName], [Customer.ParentCustomer].[ContactName], [Customer.ParentCustomer].[ContactTitle], [Customer.ParentCustomer].[ParentCustomerID]
      FROM (
          SELECT TOP(@__p_0) [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[ParentCustomerID]
          FROM [30000Records] AS [c]
      ) AS [t]
      LEFT JOIN [30000Records] AS [Customer.ParentCustomer] ON [t].[ParentCustomerID] = [Customer.ParentCustomer].[CustomerID]
      WHERE ((CASE
          WHEN [t].[ContactName] = N'xxx'
          THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
      END | CASE
          WHEN CHARINDEX(N'gio', COALESCE([t].[ContactName], N'Blanks')) > 0
          THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
      END) | CASE
          WHEN [t].[ParentCustomerID] IS NULL OR ([t].[ParentCustomerID] IS NOT NULL AND (CHARINDEX(N'gio', COALESCE([Customer.ParentCustomer].[ContactName], N'Blanks')) > 0))
          THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
      END) = 1


The highlighted part causes it to return all customers without a parentcustomer...


KO Koen January 28, 2021 03:04 PM UTC

A small question as a side note:

When I uncomment the "Take(30)" part in the following action, I get a "nullreference error":

public IActionResult UrlDatasource([FromBodyDataManagerRequest dm)
        {
            EFDataContext db = new EFDataContext();
            IQueryable<Customer> DataSource =
                db
                .Customers
                .Include(c => c.ParentCustomer)
                //.Take(30)
                ;

            QueryableOperation operation = new QueryableOperation();
            if (dm.Search != null && dm.Search.Count > 0)
            {
                DataSource = operation.PerformSearching(DataSource, dm.Search);  //Search
            }
            if (dm.Where != null && dm.Where.Count > 0//Filtering
            {
                DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
            }
            int count = DataSource.Count();
            if (dm.Skip != 0)
            {
                DataSource = operation.PerformSkip(DataSource, dm.Skip);   //Paging
            }
            if (dm.Take != 0)
            {
                DataSource = operation.PerformTake(DataSource, dm.Take);
            }
            return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);
        }

the error



why is that?


BC Berly Christopher Syncfusion Team February 2, 2021 12:48 PM UTC

Hi Koen, 
  
Sorry for the inconvenience caused, 
  
While running the shared sample, we have faced the issues at our end. So, we will check and update the details in three business days (5th February 2021). We appreciate your patience until then. 
  
Regards, 
Berly B.C 



BC Berly Christopher Syncfusion Team February 5, 2021 03:23 PM UTC

Hi Koen, 
  
Thanks for the patience.  
  
Issue 1: 
  
When searching for "Gio" I want it to show up two results: "Giovanni Rovelli" and "Anabela Domingues (child of Giovanni Rovelli)". 
However it returns "Anabela Domingues (child of Giovanni Rovelli)" AND all customers without a parent customer... 
  
Response: 
  
Due to data base version mismatch, we could not run the attached sample and access the shared data. So, we have prepared the sample with local data by shared code details and attached it below. 
  
  
Please check the sample and share the data which is loaded for the combo box component or modify the sample with the reported issue that will help us to check and proceed further at our end.  
  
Issue 2: 
  
When I uncomment the "Take(30)" part in the following action, I get a "nullreference error": 
  
Response: 
  
We suspect that some of the ComboBox field names are not present in your controller data source or the ComboBox field names may have case mismatch with your controller data source that will leads to cause the reported issue. So, please ensure the data received correctly in the dm.where[0] correctly in the server-side.  Also, it seems like common issue in C#. So, we have attached some Microsoft blogs for this issue.  
  
  
  
Regards, 
Berly B.C 



KO Koen replied to Berly Christopher February 10, 2021 07:42 AM UTC

Hi Koen, 
  
Thanks for the patience.  
  
Issue 1: 
  
When searching for "Gio" I want it to show up two results: "Giovanni Rovelli" and "Anabela Domingues (child of Giovanni Rovelli)". 
However it returns "Anabela Domingues (child of Giovanni Rovelli)" AND all customers without a parent customer... 
  
Response: 
  
Due to data base version mismatch, we could not run the attached sample and access the shared data. So, we have prepared the sample with local data by shared code details and attached it below. 
  
  
Please check the sample and share the data which is loaded for the combo box component or modify the sample with the reported issue that will help us to check and proceed further at our end.  
  
Issue 2: 
  
When I uncomment the "Take(30)" part in the following action, I get a "nullreference error": 
  
Response: 
  
We suspect that some of the ComboBox field names are not present in your controller data source or the ComboBox field names may have case mismatch with your controller data source that will leads to cause the reported issue. So, please ensure the data received correctly in the dm.where[0] correctly in the server-side.  Also, it seems like common issue in C#. So, we have attached some Microsoft blogs for this issue.  
  
  
  
Regards, 
Berly B.C 


Issue 2: 
When I uncomment the "Take(30)" part in the following action, I get a "nullreference error": 
Response:   
We suspect that some of the ComboBox field names are not present in your controller data source or the ComboBox field names may have case mismatch with your controller data source that will leads to cause the reported issue. So, please ensure the data received correctly in the dm.where[0] correctly in the server-side.  Also, it seems like common issue in C#. So, we have attached some Microsoft blogs for this issue.  

Dear,

If I leave out the Take() part, I get the null reference error; I really don't see how that should be related to "missing" fields in the controller data source... If I add Take(int.MaxValue) it is working fine... so there is something wrong with your implementation of "PerformFiltering"...

For my original question, can't you update your SQL Server Express edition to be able to open my solution?

Best regards,
Koen


BC Berly Christopher Syncfusion Team February 16, 2021 12:57 PM UTC

Hi Koen, 
  
We have created a new incident for the reported issue. So, please track the details in the incident with Direct-trac credentials.  
  
Regards, 
Berly B.C 


Loader.
Up arrow icon