linq-to-sql

EF Core 3.1 throws an exception for Contains

怎甘沉沦 提交于 2020-07-09 12:05:08
问题 I recently updated the project code into .NET Core 3.1 and EF Core 3.1, now most of my linq queries brake, EX. public override ICollection<ContactDetailModel> GetAll(ICollection<int> ids) { return _context .Set<TEntity>() .IgnoreDeletedEntities() .Where(x => ids.Distinct().Contains(x.ContactId)) .Select(EntityToDTOMapper) .ToList(); } This code throws an error where I use Contains, I saw in some other posts this issues has been fixed as a bug, but yet it fails. Error I get is "could not be

“Between” in Linq C# [duplicate]

做~自己de王妃 提交于 2020-06-25 09:13:32
问题 This question already has an answer here : Closed 9 years ago . Possible Duplicate: LINQ Between Operator Dear All, Hi, I need to write this query in LINQ C#. can anyone help me? Select * From Mytable where MyText BETWEEN 'john' AND 'Pear' 回答1: I believe this query should work: var results = yourTable.Where(x => x.Text.CompareTo("john") > 0 && x.Text.CompareTo("Pear") < 0); This assumes that you want to compare the text in each row of the table, and not some pre-dfined string. 回答2: Here is

Casting to Decimal is not supported in LINQ to Entities queries

只谈情不闲聊 提交于 2020-06-10 02:54:27
问题 I have a database table Transaction (transactionID, LocalAmount...). where datatype for Localamount property is float . ON the UI I am trying to return a SUM of column (Localamount) in one row on a button click event. I have used decimal instead of float However I am getting an error on the code where I am casting to decimal System.NotSupportedException was unhandled by user code Message=Casting to Decimal is not supported in LINQ to Entities queries, because the required precision and scale

How can I determine if a LINQ query is going to be LINQ to SQL vs. LINQ to Objects?

£可爱£侵袭症+ 提交于 2020-05-13 04:57:51
问题 Usually the distinction between LINQ to SQL and LINQ to Objects isn't an issue (and I think making it an issue from the get-go is premature optimization), but how can I determine which is happening? It would be useful to know when writing the code, but I fear one can only be sure at run time sometimes. 回答1: It's not micro optimization to make the distinction between Linq-To-Sql and Linq-To-Objects. The latter requires all data to be loaded into memory before you start filtering it. Of course,

The type of one of the expressions in the join clause is incorrect in Entity Framework. Constant in left join

限于喜欢 提交于 2020-04-21 08:14:14
问题 I'm trying to do a left join in an EF query. I'm getting the following error: Error CS1941 The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin' and here is the C# code: var foo = from m in db.ClientMasters join a in db.Orders on new { m.Id, Status = "N" } equals new { a.ClientID, a.Status } into a_join from a in a_join.DefaultIfEmpty() select new { m.ClientID, a.ID }; 回答1: I'm an idiot. The column names have to match in the join.

Entity Framework 3.0 Contains cannot be translated in SQL as it was in EF Core 2.2

旧城冷巷雨未停 提交于 2020-04-10 14:04:07
问题 I am trying to migrate a Web API from .NET Core 2.2 to .NET Core 3.0 and I have stumbled across the following: public Dictionary<int, Tag> GetTagMap(IList<int> tagIds = null) { var tags = context.Tag.AsNoTracking(); if (tagIds != null) tags = tags.Where(t => tagIds.Contains(t.TagId)); return tags .ToList() // explicit client evaluation in 3.0 .ToDictionary(t => t.TagId, t => t); } This used to generate a SQL statement similar to this one: SELECT TagId, Name FROM Tag WHERE TagId IN (1, 2, 3)

Entity Framework 3.0 Contains cannot be translated in SQL as it was in EF Core 2.2

被刻印的时光 ゝ 提交于 2020-04-10 14:03:18
问题 I am trying to migrate a Web API from .NET Core 2.2 to .NET Core 3.0 and I have stumbled across the following: public Dictionary<int, Tag> GetTagMap(IList<int> tagIds = null) { var tags = context.Tag.AsNoTracking(); if (tagIds != null) tags = tags.Where(t => tagIds.Contains(t.TagId)); return tags .ToList() // explicit client evaluation in 3.0 .ToDictionary(t => t.TagId, t => t); } This used to generate a SQL statement similar to this one: SELECT TagId, Name FROM Tag WHERE TagId IN (1, 2, 3)

Entity Framework 3.0 Contains cannot be translated in SQL as it was in EF Core 2.2

半腔热情 提交于 2020-04-10 14:02:06
问题 I am trying to migrate a Web API from .NET Core 2.2 to .NET Core 3.0 and I have stumbled across the following: public Dictionary<int, Tag> GetTagMap(IList<int> tagIds = null) { var tags = context.Tag.AsNoTracking(); if (tagIds != null) tags = tags.Where(t => tagIds.Contains(t.TagId)); return tags .ToList() // explicit client evaluation in 3.0 .ToDictionary(t => t.TagId, t => t); } This used to generate a SQL statement similar to this one: SELECT TagId, Name FROM Tag WHERE TagId IN (1, 2, 3)

Using Linq to Sql asynchronously with new async/await

柔情痞子 提交于 2020-04-09 21:06:54
问题 What are best practices to use L2S with new C# 5 async/await keywords comparing to this approach? Couldn't find any on web. 回答1: EF 5 does not have async/await support, but the open source version is actively looking into possibilities here. EDIT: the Async support in EF is documented at http://msdn.microsoft.com/en-us/data/jj819165.aspx. It doesn't stream the results in as they are hydrated (as you would find with RX) but it does make the database calls asynchronous. As for LINQ to SQL,

Using Linq to Sql asynchronously with new async/await

这一生的挚爱 提交于 2020-04-09 21:01:33
问题 What are best practices to use L2S with new C# 5 async/await keywords comparing to this approach? Couldn't find any on web. 回答1: EF 5 does not have async/await support, but the open source version is actively looking into possibilities here. EDIT: the Async support in EF is documented at http://msdn.microsoft.com/en-us/data/jj819165.aspx. It doesn't stream the results in as they are hydrated (as you would find with RX) but it does make the database calls asynchronous. As for LINQ to SQL,