locked
Union IQueryable Error RRS feed

  • Question

  • User-957910121 posted

    Hello,

    I have an error coming up in my select statment which includes a union. I've tried declaring ToList() but it's not helped.

    The error is:

    Error	6	
    Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 
    'System.Linq.IQueryable<Spoton_Areas_Test.Models.tbl_vessels>'. An explicit conversion exists (are you missing a cast?)

    Here is the select statement.  Can anyone shed some like on where I've gone wrong and how I can fix it?

      IQueryable<VesselsViewModel> thevessel = (from p in datacontext
            
                                                          where p.fixture_stop == null ||
                                                                p.fixture_start < currentDate &&
                                                                p.fixture_stop < futureDate
    
                                                          select new 
                                                          {
                                                              vessel_idx = p.vessel_idx,
                                                              vessel_name = p.vessel_name,
                                                              fixture_charterer = "",
                                                              fixture_work = "",
                                                              fixture_location = ""
                                                          }
                                                          ).Union(
                                                            (from p in datacontext
                                                             where p.fixture_start >= currentDate
                                                             select new 
                                                             {
                                                                 vessel_idx = p.vessel_idx,
                                                                 vessel_name = p.vessel_name,
                                                                
                                                                 fixture_charterer = "",
                                                                 fixture_work = "",
                                                                 fixture_location = ""                                                                                                                    }
                                                             ));

    Many thanks

    Wednesday, April 6, 2016 11:20 AM

Answers

  • User1559292362 posted

    Hi Yanayaya,

    I have an error coming up in my select statment which includes a union. I've tried declaring ToList() but it's not helped.

    According to your code snippet, you want to retrieve an IQueryable with type of VesselsViewModel, but in your Linq statement, you return a Type of Anonymous, which cause the issue. Please modify your code snippet like this:

    IQueryable<VesselsViewModel> thevessel = (from p in datacontext
    
                                                          where p.fixture_stop == null ||
    
                                                                p.fixture_start < currentDate &&
    
                                                                p.fixture_stop < futureDate
    
     
    
                                                          select new VesselsViewModel
    
                                                          {
    
                                                              vessel_idx = p.vessel_idx,
    
                                                              vessel_name = p.vessel_name,
    
                                                              fixture_charterer = "",
    
                                                              fixture_work = "",
    
                                                              fixture_location = ""
    
                                                          }
    
                                                          ).Union(
    
                                                            (from p in datacontext
    
                                                             where p.fixture_start >= currentDate
    
                                                             select new VesselsViewModel
    
                                                             {
    
                                                                 vessel_idx = p.vessel_idx,
    
                                                                 vessel_name = p.vessel_name,
    
                                                                 fixture_charterer = "",
    
                                                                 fixture_work = "",
    
                                                                 fixture_location = ""                                                                                                                    }
    
                                                             ));
    

    Or

    var thevessel = (from p in datacontext
    
                                                          where p.fixture_stop == null ||
    
                                                                p.fixture_start < currentDate &&
    
                                                                p.fixture_stop < futureDate
    
                                                          select new
    
                                                          {
    
                                                              vessel_idx = p.vessel_idx,
    
                                                              vessel_name = p.vessel_name,
    
                                                              fixture_charterer = "",
    
                                                              fixture_work = "",
    
                                                              fixture_location = ""
    
                                                          }
    
                                                          ).Union(
    
                                                            (from p in datacontext
    
                                                             where p.fixture_start >= currentDate
    
                                                             select new
    
                                                             {
    
                                                                 vessel_idx = p.vessel_idx,
    
                                                                 vessel_name = p.vessel_name,
    
                                                                 fixture_charterer = "",
    
                                                                 fixture_work = "",
    
                                                                 fixture_location = ""                                                                                                                    }
    
                                                             ));
    

     

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 7, 2016 8:18 AM