linq-to-sql

using .Contains() with linq-to-sql

点点圈 提交于 2020-07-19 04:28:13
问题 I have the following query that receives a list of ints as a parameter: public int GetMostRecent(List<int> TheIDs) { ...using MyDC... var TheMostRecentID = (from d in MyDC.Data where TheIDs.Contains(d.ID) orderby d.DateTime select d.ID).LastOrDefault(); } Is this the best way to match a list within a parameter collection to data in the database or is there a better way than using the .Contains() method in linq-to-sql. Thanks. 回答1: What you have is correct. This will be translated into an IN

LINQ Multiple LIKE based on List

为君一笑 提交于 2020-07-15 06:18:08
问题 I have a list of keywords in an ArrayList and I wanted to be able to build a query to find records in a table based on this keywords. Since the list of keywords is dynamic I cannot build a fixed query here. I do something like this: foreach (string kw in keywords) { query = query.Where(p => p.Name.StartsWith(kw)); } The "StartsWith" is required here because I need to search those records that actually start with the provided keyword. In T-SQL it Would be something like this Select * from

LINQ Multiple LIKE based on List

扶醉桌前 提交于 2020-07-15 06:17:04
问题 I have a list of keywords in an ArrayList and I wanted to be able to build a query to find records in a table based on this keywords. Since the list of keywords is dynamic I cannot build a fixed query here. I do something like this: foreach (string kw in keywords) { query = query.Where(p => p.Name.StartsWith(kw)); } The "StartsWith" is required here because I need to search those records that actually start with the provided keyword. In T-SQL it Would be something like this Select * from

EF Core 3.1 throws an exception for Contains

柔情痞子 提交于 2020-07-09 12:05:22
问题 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