locked
Two C# linq queries convert to vb.net RRS feed

  • Question

  • Hi guys,
    have some C# linq and would like to convert it to vb.net linq. Can you help?

      
      var currentUserId = 25;  
        var prjectId = 10;  
        
        var projects= db.tbProjekt.Where(s => s.tbUserProjects
                                               .Any(x => x.UserId == currentUserId)).ToList();
        
        
        var projectIds = db.tbUserProjects
                            .Where(s => s.UserId == currentUserId && s.ProjectId == idPojektu)
                            .Select(x => x.ProjectId).ToList();
    



    in first case i tried:

      
       var currentUserId = 25;  
                        ProjectsPerUser = db.tbProjekt.Where(Function(s) s.tbUserProjects.Any(Function(x) x.UserId = currentUserId )).ToList()


     

    but i got error:

    `"Late binding operationscannot be converted toexpression tree"` on line  s.tbUserProjects.Any
    • Edited by Johnyyy Thursday, December 17, 2015 7:11 PM
    Thursday, December 17, 2015 7:11 PM

Answers

  • Hi johnyyy,

    The problem maybe is that you are trying to use strongly typed. Try to modify code snippet as below.

    Dim currentUserId = 25
    
    Dim perUser = db.tbProjekt.Where(Function(s) s.tbUserProjects.Any(Function(x) x.UserId = currentUserId)).ToList()
    

    Best regards,

    • Edited by Fred Bao Wednesday, December 23, 2015 1:41 AM
    • Proposed as answer by Fred Bao Monday, December 28, 2015 7:28 AM
    • Marked as answer by Fred Bao Tuesday, December 29, 2015 8:26 AM
    Friday, December 18, 2015 8:57 AM

All replies

  • Hi johnyyy,

    I reproduced you code snippet and made a demo, it worked fine. Below is my demo for you
    reference.

    Dim db As New DemoEntities
    Dim productId = 1
    Dim suplies= (db.Suppliers.Where(Function(s) s.Products.Any(Function(x) x.Id = productId))).ToList()
    Console.WriteLine(suplies.Count)
    Console.ReadKey()

    Hope it is helpful to you.

    Best regards,



    • Edited by Fred Bao Wednesday, December 23, 2015 1:38 AM
    Friday, December 18, 2015 3:16 AM
  • that's exactly how i did and i amr eceiving error :

    Late binding operationscannot be converted toexpression tree"` on line  s.tbUserProjects.Any

    Friday, December 18, 2015 8:05 AM
  • Hi johnyyy,

    The problem maybe is that you are trying to use strongly typed. Try to modify code snippet as below.

    Dim currentUserId = 25
    
    Dim perUser = db.tbProjekt.Where(Function(s) s.tbUserProjects.Any(Function(x) x.UserId = currentUserId)).ToList()
    

    Best regards,

    • Edited by Fred Bao Wednesday, December 23, 2015 1:41 AM
    • Proposed as answer by Fred Bao Monday, December 28, 2015 7:28 AM
    • Marked as answer by Fred Bao Tuesday, December 29, 2015 8:26 AM
    Friday, December 18, 2015 8:57 AM