none
How do I compare two different Models using LINQ RRS feed

  • Question

  • The code below shows two different data sources, one is a Webservice and another is a local Database.  What I'm trying to accomplish here is to select the product in the Remote DB where the items are not in the local DB.  in SQL, I Would run the following script.

    Select rm.ProductID from RemoteLink.Database.dbo.Product rm where rm.ProductID not in

    (

    Select matnum as ProductID from Database.dbo.matm

    )

    So how do I write it in LINQ query.  This is my current code below.  Thanks alot

    LINQ Code

                // Remote DB
                dbEntities _remoteService = new dbEntities(new Uri("http://localhost:51348/InventoryServices/ProductService.svc/"));
                var remoteProducts = (from p in _remoteService.Product
                                where p.statusCode == "O"
                                orderby p.productId descending
                                select p
                    ).ToList();

                // Local DB
                ClientDbPOSEntities _localService = new ClientDbPOSEntities();
                var localProducts = (from p in _localService.matm
                                     where p.status == "O"
                                     orderby p.matnum descending
                                     select p
                    ).ToList();


    Tuesday, April 4, 2017 8:33 AM

All replies

  •                   
    You cannot vote on your own post
    0

    The code below shows two different data sources, one is a Webservice and another is a local Database.  What I'm trying to accomplish here is to select the product in the Remote DB where the items are not in the local DB.  in SQL, I Would run the following script.

    Select rm.ProductID from RemoteLink.Database.dbo.Product rm where rm.ProductID not in

    (

    Select matnum as ProductID from Database.dbo.matm

    )

    So how do I write it in LINQ query.  This is my current code below.  Thanks alot

    LINQ Code

                // Remote DB
                dbEntities _remoteService = new dbEntities(new Uri("http://localhost:51348/InventoryServices/ProductService.svc/"));
                var remoteProducts = (from p in _remoteService.Product
                                where p.statusCode == "O"
                                orderby p.productId descending
                                select p
                    ).ToList();

                // Local DB
                ClientDbPOSEntities _localService = new ClientDbPOSEntities();
                var localProducts = (from p in _localService.matm
                                     where p.status == "O"
                                     orderby p.matnum descending
                                     select p
                    ).ToList();

    Tuesday, April 4, 2017 8:37 AM
  • Hi Jason Alerta Obina,

    Please try the following code.

     var result = _remoteService.Where(p => !_localService.Any(p2 => p2.matnum == p.ProductID)).Select(t=>t.ProductID);

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 5, 2017 5:49 AM
    Moderator