sql-to-linq-conversion

Multiple Join and Group By LINQ

做~自己de王妃 提交于 2019-12-06 09:19:39
问题 I need to generate this data model (example): IList<FeatureGroupFeaturesDto> fcf = new List<FeatureGroupFeaturesDto>(); fcf.Add(new FeatureGroupFeaturesDto { FeatureGroup = new FeatureGroupDto { Id = 1, Name = "Interior" }, Features = new List<FeatureDto> { new FeatureDto { Id = 7, Name = "Bancos Traseiros Rebatíveis" }, new FeatureDto { Id = 35, Name = "Computador de Bordo" }, new FeatureDto { Id = 38, Name = "Suporte para Telemóvel" } }, }); fcf.Add(new FeatureGroupFeaturesDto {

Multiple Join and Group By LINQ

柔情痞子 提交于 2019-12-04 16:58:55
I need to generate this data model (example): IList<FeatureGroupFeaturesDto> fcf = new List<FeatureGroupFeaturesDto>(); fcf.Add(new FeatureGroupFeaturesDto { FeatureGroup = new FeatureGroupDto { Id = 1, Name = "Interior" }, Features = new List<FeatureDto> { new FeatureDto { Id = 7, Name = "Bancos Traseiros Rebatíveis" }, new FeatureDto { Id = 35, Name = "Computador de Bordo" }, new FeatureDto { Id = 38, Name = "Suporte para Telemóvel" } }, }); fcf.Add(new FeatureGroupFeaturesDto { FeatureGroup = new FeatureGroupDto { Id = 2, Name = "Exterior" }, Features = new List<FeatureDto> { new FeatureDto

Checking for Nulls on DB Record Mapping

送分小仙女□ 提交于 2019-12-04 15:34:12
How can I check for db null values in the attached code? Please understand I am a new C# convert... What this code does is takes a IDataReader object and converts and maps it to a strongly-typed list of objects. But what I am finding is it completely errors out when there are null columns returned in the reader. Converter internal class Converter<T> where T : new() { // Declare our _converter delegate readonly Func<IDataReader, T> _converter; // Declare our internal dataReader readonly IDataReader dataReader; // Build our mapping based on the properties in the class/type we've passed in to the

Using LINQ find nearby places from database

这一生的挚爱 提交于 2019-12-04 07:38:17
We want to receive list of nearby places from database using LINQ in ASP.NET 2012 and would like some feedback on our strategy. My table and fake data: PlaceId Name Latitude Longitude 1 A 18.1 20.1 2 B 18.2 20.2 3 C 18.3 20.3 1) In our project the client current location (latitude and longitude) is taken as input 2) At server side ,depending upon the client current location, we need to find nearby places from the database using LINQ Here's the code for SQL which I earlier used , but now we want to use LINQ. SELECT name, Latitude, Longitude , ( 3959 * acos( cos( radians(?) )* cos( radians(

Convert SQL query with join to lambda expression

戏子无情 提交于 2019-12-02 05:31:57
问题 not sure how to convert the following sql into a lambda expression. My database uses referential integrity and table Content related to table Content_Training in a 1 to many relationship (1 content can have many content_trainings) select c.ContentId, c.Name, ct.TrainingTypeId from dbo.Content c left join dbo.Content_Training ct on c.ContentId = ct.ContentId where c.PublishDate is not null order by ct.TrainingTypeId, c.Name 回答1: Try this query: var results = (from c in dbcontext.Contents join

Convert SQL query with join to lambda expression

我只是一个虾纸丫 提交于 2019-12-02 02:41:16
not sure how to convert the following sql into a lambda expression. My database uses referential integrity and table Content related to table Content_Training in a 1 to many relationship (1 content can have many content_trainings) select c.ContentId, c.Name, ct.TrainingTypeId from dbo.Content c left join dbo.Content_Training ct on c.ContentId = ct.ContentId where c.PublishDate is not null order by ct.TrainingTypeId, c.Name Raja Nadar Try this query: var results = (from c in dbcontext.Contents join ct in dbcontext.Content_Trainings on c.ContentId equals ct.ContentId into t from rt in t

Convert SQL into Linq query

本小妞迷上赌 提交于 2019-12-01 23:41:08
I'm quite new at Linq queries, I just want to convert my DB query into Linq. Here is my simple SQL query: var query = "SELECT EnrollmentDate, COUNT(*) AS StudentCount " + "FROM Person " + "WHERE EnrollmentDate IS NOT NULL " + "GROUP BY EnrollmentDate"; var data = db.Database.SqlQuery<EnrollmentDateGroup>(query); It is working fine , but how could it possible to write this query in Linq , I just can't convert the group by statement into Linq. It seems somewhat tricky to convert into Linq. Can anyone help me with this? Habib var result = db.Person .Where(r=> r.EnrollmentDate != null) .GroupBy(r=

Linq union usage?

可紊 提交于 2019-11-27 23:59:43
Sql: SELECT date,total_usage_T1 as TotalUsageValue,'T1' as UsageType FROM TblSayacOkumalari UNION ALL SELECT date,total_usage_T2 as TotalUsageValue,'T2' as UsageType FROM TblSayacOkumalari And I try to do to convert it to linq IEnumerable<TblSayacOkumalari> sayac_okumalari = entity.TblSayacOkumalari .Select(x => new { x.date, x.total_usage_T1 }) .Union(entity.TblSayacOkumalari.Select(x => new { x.date, x.total_usage_T2 })); But I dont know how to convert 'T1' as UsageType to linq. Also my union using is incorrect too. My table fields like this: | date | total_usage_T1 | total_usage_T2 | | 2010

Linq union usage?

a 夏天 提交于 2019-11-26 21:40:15
问题 Sql: SELECT date,total_usage_T1 as TotalUsageValue,'T1' as UsageType FROM TblSayacOkumalari UNION ALL SELECT date,total_usage_T2 as TotalUsageValue,'T2' as UsageType FROM TblSayacOkumalari And I try to do to convert it to linq IEnumerable<TblSayacOkumalari> sayac_okumalari = entity.TblSayacOkumalari .Select(x => new { x.date, x.total_usage_T1 }) .Union(entity.TblSayacOkumalari.Select(x => new { x.date, x.total_usage_T2 })); But I dont know how to convert 'T1' as UsageType to linq. Also my

SQL to LINQ with multiple join, count and left join

半世苍凉 提交于 2019-11-25 21:47:17
问题 I wrote this SQL request with multiple JOIN (including a LEFT JOIN ). It gives me the expected result . SELECT DISTINCT c.Id, c.Title, COUNT(v.Id) AS \'Nb_V2\', COUNT(DISTINCT v.IdUser) AS \'Nb_V1\', r.cnt AS \'Nb_R\' FROM TABLE_C c JOIN TABLE_V v on c.Id = v.Id LEFT JOIN ( SELECT Id, COUNT(*) AS cnt FROM TABLE_R GROUP BY Id ) r ON c.Id = r.Id WHERE c.IdUser = \'1234\' GROUP BY c.Id, c.Title, r.cnt However, \'Id like the Linq equivalent of this request, to put it my application\'s Data Access