none
How do I compare two different Models using LINQ

    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:36 AM

Answers

  • You can achieve this in combination with Where and Any() like:

    var result = remoteProducts.Where(p=> !localProducts.Any(x=>x.matnum == p.ProductId)).ToList();

    This will return you the products from the remote products list which are not in local products list.

    Hope it helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, April 4, 2017 9:15 AM

All replies

  • on which property basis you decide that the two are same products, ProductID ?

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, April 4, 2017 8:57 AM
  • In the remote DB it's ProductID and in the Local DB it's matnum. Thanks

    Tuesday, April 4, 2017 9:04 AM
  • You can achieve this in combination with Where and Any() like:

    var result = remoteProducts.Where(p=> !localProducts.Any(x=>x.matnum == p.ProductId)).ToList();

    This will return you the products from the remote products list which are not in local products list.

    Hope it helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, April 4, 2017 9:15 AM
  • Hey thanks... it worked :D
    Wednesday, April 5, 2017 4:14 AM