locked
Help needed in LINQ query RRS feed

  • Question

  • User-48430401 posted

    Hi Guys,

     i have a problem in a query, i am trying to split a string with comma and then i am trying to compare all splitted values with the list of sessions, here is the code 

     public static CorporateInvoice SelectInvoiceByCorporateAndSession(int CorporateID, List<int> SessionID)
     {
                ELSPortalDataContext db = new ELSPortalDataContext();
                return db.CorporateInvoices.SingleOrDefault(o => o.CorporateID == CorporateID && o.ObjectID.Split(',').ToList().Any(c => SessionID.Select(k => k.ToString()).Contains(c)));
            
    }

    i am unable to do and getting error "Method 'System.String[] Split(Char[])' has no supported translation to SQ"

    Please help.

    Thanks

    Tuesday, May 21, 2013 10:22 AM

Answers

  • User281315223 posted

    If memory serves, when you encounter this issue you often have to create a concrete instance of your collection so that methods that aren't limited to SQL functionality can be used within them.

    You'll just need to add a Enumerable.ToList() or Enumerable.AsEnumerable() method following your actual collection of CorporateInvoices as seen below : 

    public static CorporateInvoice SelectInvoiceByCorporateAndSession(int CorporateID, List<int> SessionID)
    {
           ELSPortalDataContext db = new ELSPortalDataContext();
    
           //Create a concrete implementation of your collection (using ToList())
           return db.CorporateInvoices.ToList().SingleOrDefault(o => o.CorporateID == CorporateID && o.ObjectID.Split(',').ToList().Any(c => SessionID.Select(k => k.ToString()).Contains(c)));
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 21, 2013 10:25 AM

All replies

  • User281315223 posted

    If memory serves, when you encounter this issue you often have to create a concrete instance of your collection so that methods that aren't limited to SQL functionality can be used within them.

    You'll just need to add a Enumerable.ToList() or Enumerable.AsEnumerable() method following your actual collection of CorporateInvoices as seen below : 

    public static CorporateInvoice SelectInvoiceByCorporateAndSession(int CorporateID, List<int> SessionID)
    {
           ELSPortalDataContext db = new ELSPortalDataContext();
    
           //Create a concrete implementation of your collection (using ToList())
           return db.CorporateInvoices.ToList().SingleOrDefault(o => o.CorporateID == CorporateID && o.ObjectID.Split(',').ToList().Any(c => SessionID.Select(k => k.ToString()).Contains(c)));
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 21, 2013 10:25 AM
  • User-48430401 posted

    Thanks alot Rion, it really works now...thanks again.

    Tuesday, May 21, 2013 10:31 AM