entity-framework

How to convert from decimal to double in Linq to Entity

送分小仙女□ 提交于 2020-05-16 03:38:53
问题 Suppose we have table T which has two columns A and B with float and money types respectively. I want to write a linq query like following T-SQL statement: Select A, B, A * B as C From SomeTable Where C < 1000 I tried to cast like following var list = (from row in model.Table where ((decimal)row.A) * row.B < 1000 select new { A = row.A, B = row.B , C = ((decimal)row.A) * row.B} ).ToList(); but it does not allow the cast operation. It throw an exception: Casting to Decimal is not supported in

How to efficiently retrieve a list of random elements from an IQueryable?

一个人想着一个人 提交于 2020-05-15 11:39:49
问题 I've seen the following: Random element of List<T> from LINQ SQL What I'm doing now - to get three random elements - is the following: var user1 = users.ElementAt( rand.Next( users.Count() ) ); var user2 = users.Where(u => !u.Equals(user1)).ElementAt( rand.Next( users.Count() ) ); var user3 = users.Where(u => !u.Equals(user1) && !u.Equals(user2)).ElementAt( rand.Next( users.Count() ) ); But this is obviously unweildy and inefficient. How can I do this elegantly and efficiently with one trip

c# linq with group by and join throwing exception

岁酱吖の 提交于 2020-05-15 08:40:10
问题 (from p in this.m_dbContext.Patient join b in (from a in this.m_dbContext.Appointments join p in this.m_dbContext.Patient on a.Patientid equals p.Patientid where a.Doctorid == doctorid && a.Clinicid == clinicid group a by a.Patientid) on p.Patientid equals b.FirstOrDefault().Patientid orderby p.Name select new { p.Patientid, p.Clinicid, p.Name, p.Mobilenumber, p.Gender, p.Dob, p.Age, p.Address, p.City, p.State, p.Pincode }).ToList().Count(); I get the below exception when i run, i use group

c# linq with group by and join throwing exception

喜夏-厌秋 提交于 2020-05-15 08:40:00
问题 (from p in this.m_dbContext.Patient join b in (from a in this.m_dbContext.Appointments join p in this.m_dbContext.Patient on a.Patientid equals p.Patientid where a.Doctorid == doctorid && a.Clinicid == clinicid group a by a.Patientid) on p.Patientid equals b.FirstOrDefault().Patientid orderby p.Name select new { p.Patientid, p.Clinicid, p.Name, p.Mobilenumber, p.Gender, p.Dob, p.Age, p.Address, p.City, p.State, p.Pincode }).ToList().Count(); I get the below exception when i run, i use group

The LINQ expression could not be translated and will be evaluated locally

自古美人都是妖i 提交于 2020-05-14 19:27:09
问题 Im getting this WARNING in EntityFramework Core what is wrong? I already set MSSQL Datebase to Case Sensitive. Latin1_General_100_CS_AS var test = await _context.Students .FirstOrDefaultAsync(m => m.LastName.Equals("ALEXANDER", StringComparison.InvariantCultureIgnoreCase)); Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression 'where [m].LastName.Equals("ALEXANDER", InvariantCultureIgnoreCase)' could not be translated and will be evaluated locally. 回答1: You have to be aware of the

Entity Framework Code First - Cast smallint and integer to int32

柔情痞子 提交于 2020-05-14 18:03:19
问题 Im working on a small service tool for a database. My problem is that due the last update, some smallint-colums had to be be changed into integer. public class TEST { public int ID { get; set; } //public Int16 ID { get; set; } public string TEST { get; set; } } I changed the type from Int16 to int. Everything works fine, except that I can't use it with the old Version of the Database anymore. The Exception is something like "System.Int32 expected, found Typ System.Int16". Is there a way to

How to adapt IObjectContextAdapter from EF 6 to EF Core

核能气质少年 提交于 2020-05-14 16:24:50
问题 I am trying to port this class to EF core: https://github.com/mehdime/DbContextScope/blob/master/Mehdime.Entity/Implementations/DbContextScope.cs However I am having this issue: Error CS0246: The type or namespace name 'IObjectContextAdapter' could not be found (are you missing a using directive or an assembly reference?) (CS0246) (Mehdime.Entity) and this: Error CS0246: The type or namespace name 'ObjectStateEntry' could not be found (are you missing a using directive or an assembly

Join in LINQ and Entity Framework

☆樱花仙子☆ 提交于 2020-05-14 16:17:30
问题 In SQL I to get the distinct statement, I used join to get it as below select distinct col1 from table1 a inner join table2 b on a.code = b.vcode How can the same be implemented in linq over Entity Framework? Please suggest me. 回答1: var result = (from a in table1 join b in table2 on a.code equals b.vcode select a.col1).Distinct(); 回答2: You can also use method syntax: var query = table1.Join(table2, a => a.code, b => b.vcode, (a,b) => a.col1) .Distinct(); 来源: https://stackoverflow.com

Join in LINQ and Entity Framework

╄→尐↘猪︶ㄣ 提交于 2020-05-14 16:14:31
问题 In SQL I to get the distinct statement, I used join to get it as below select distinct col1 from table1 a inner join table2 b on a.code = b.vcode How can the same be implemented in linq over Entity Framework? Please suggest me. 回答1: var result = (from a in table1 join b in table2 on a.code equals b.vcode select a.col1).Distinct(); 回答2: You can also use method syntax: var query = table1.Join(table2, a => a.code, b => b.vcode, (a,b) => a.col1) .Distinct(); 来源: https://stackoverflow.com

Where are Entity Framework Tools in EF6 and Visual Studio 2017?

不羁的心 提交于 2020-05-14 14:48:10
问题 I'm using Visual Studio 2017 and Entity Framework 6, and for the life of me, I cannot find the EDMX file, Entity Data Tools, Model Browser, or Entity Designer... Am I losing my mind here? I've created multiple new projects trying to find these UI-based tools for working with the Entity Framework data models and can't seem to get anywhere. Please help! I believe I have the correct tools installed in the VS2017 Install: 回答1: For anyone looking at this, in my case the answer was that I was using