Search code examples
c#linqlinq-to-datasetlinq-query-syntax

When using Query Syntax in C# "Enumeration yielded no results". How to retrieve output


I have created this query to fetch some result from database. Here is my table structure.

What exaclty is happening.

DtMapGuestDepartment as Table 1

DtDepartment as Table 2

Are being used

    var dept_list=  from map in DtMapGuestDepartment.AsEnumerable()
                    where map.Field<Nullable<long>>("GUEST_ID") == DRowGuestPI.Field<Nullable<long>>("PK_GUEST_ID")
                    join 
                    dept in DtDepartment.AsEnumerable()
                    on map.Field<Nullable<long>>("DEPARTMENT_ID") equals dept.Field<Nullable<long>>("DEPARTMENT_ID")
                    select dept.Field<string>("DEPARTMENT_ID");

I am performing this query on DataTables and expect it to return me a datatable.

Here I want to select distinct department from Table 1 as well which will be my next quest. Please answer to that also if possible.


Solution

  • break your query into parts and see which collection has no elements.

    var mapList = DtMapGuestDepartment.AsEnumerable().ToList();
    var deptList = DtDepartment.AsEnumerable().ToList();
    
    var queryResult1 = (
      from map in mapList
      where map.Field<Nullable<long>>("GUEST_ID") ==
        DRowGuestPI.Field<Nullable<long>>("PK_GUEST_ID") 
      select map
    ).ToList();
    
    var queryResult2 = (
      from map in queryResult1
      join dept in deptList
        on map.Field<Nullable<long>>("DEPARTMENT_ID")
        equals dept.Field<Nullable<long>>("DEPARTMENT_ID") 
      select dept.Field<string>("DEPARTMENT_ID")
    ).ToList();