Donate

LINQ To SQL In Operator With Subquery

Given the following sql statement below using In operator with subquery as it's argument, I need to convert this to LINQ statement.
SELECT [CountryRegionCode]
      ,[Name]
      ,[ModifiedDate]
  FROM [AdventureWorks2012].[Person].[CountryRegion]
  where CountryRegionCode in (select distinct [CountryRegionCode] 
  from [AdventureWorks2012].[Person].StateProvince)
After doing some readings on LINQ documentation, I found it's equivalent LINQ code using let.
model.CountryRegionsList 
     = (from country in _context.CountryRegions
       let subQuery = (from stateprovince in _context.StateProvinces
                      select stateprovince.CountryRegionCode).Distinct()
       where subQuery.Contains(country.CountryRegionCode)
       select country).ToList();

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.