linq-to-entities

How to stay DRY whilst using LINQ to Entities and helper methods?

让人想犯罪 __ 提交于 2020-01-12 15:17:44
问题 Lets say that I have a particular way of deciding whether some strings "match", like this: public bool stringsMatch(string searchFor, string searchIn) { if (string.IsNullOrEmpty(searchFor)) { return true; } return searchIn != null && (searchIn.Trim().ToLower().StartsWith(searchFor.Trim().ToLower()) || searchIn.Contains(" " + searchFor)); } I would like to pull matches out of a database using Linq To Entities and this helper. However, when I try this: IQueryable<Blah> blahs = query.Where(b =>

LINQ Guid toString()

烈酒焚心 提交于 2020-01-12 06:52:45
问题 Hi this seems like it should work, from something in collectionofsomestuff select new SelectListItem(){Text = something.Name, Value = something.SomeGuid.ToString(), Selected = false}; When I try to do this it doesn't work give me error LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. Is there a workaround? 回答1: Not all CLR methods can be used with Linq-to-Entities. ToString() seems to be one of them

LINQ Query returns multiple copies of first result

别来无恙 提交于 2020-01-11 09:35:47
问题 I have a view defined in the database (archiveContentPreviews), It joins together several tables and in Linq it has one entity key (ArchiveID), I want to query this view with this simple query: var x = from fields in entities2.archiveContentPreviews where fields.ArchiveID == archiveID select fields; return x.ToList<archiveContentPreview>(); The problem that it returns exact number of results but multiple copy of the first result, and when I execute that query in SQL management studio it

When quering over a base type why does the EF provider generate all those UNION All clauses

纵然是瞬间 提交于 2020-01-09 12:03:25
问题 If I have a model that looks like this: and I do a Linq to Entities query like this: var c = MyContext.Contact.Count(); I'll get a SQL query that is as big as all out doors! SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT COUNT(1) AS [A1] FROM [dbo].[Contacts] AS [Extent1] LEFT OUTER JOIN (SELECT [UnionAll1].[Id] AS [C1] FROM (SELECT [Extent2].[Id] AS [Id] FROM [dbo].[Companies] AS [Extent2] UNION ALL SELECT [Extent3].[Id] AS [Id] FROM [dbo].[Employees] AS [Extent3]) AS [UnionAll1] UNION ALL

IQueryable extension method for linq2entities

本小妞迷上赌 提交于 2020-01-09 09:43:19
问题 I am trying to implement an extension method that will work with linq2entities. I had initially assumed that if my extension method took and returned an IQueryable, and as long as my expression only made use of supported methods, then it would work fine. I was having alot of trouble, so as a last resort I copied an existing .NET extension method that I knew to work(FirstOrDefault) and simply renamed it. It would seem like it would evaluate the "cannot be translated into a store expression"

IQueryable extension method for linq2entities

删除回忆录丶 提交于 2020-01-09 09:42:30
问题 I am trying to implement an extension method that will work with linq2entities. I had initially assumed that if my extension method took and returned an IQueryable, and as long as my expression only made use of supported methods, then it would work fine. I was having alot of trouble, so as a last resort I copied an existing .NET extension method that I knew to work(FirstOrDefault) and simply renamed it. It would seem like it would evaluate the "cannot be translated into a store expression"

IQueryable extension method for linq2entities

天大地大妈咪最大 提交于 2020-01-09 09:41:25
问题 I am trying to implement an extension method that will work with linq2entities. I had initially assumed that if my extension method took and returned an IQueryable, and as long as my expression only made use of supported methods, then it would work fine. I was having alot of trouble, so as a last resort I copied an existing .NET extension method that I knew to work(FirstOrDefault) and simply renamed it. It would seem like it would evaluate the "cannot be translated into a store expression"

Can I mix Table per Hierarchy and Table per Type in Entity Framework?

本小妞迷上赌 提交于 2020-01-09 08:08:06
问题 Say I have 2 tables: Message and SuperMessage and 3 entities: Message (base (not abstract)), Comment (inherits from Message) and SuperMessage (inherits from Message) Message has a non-nullable MessageType field which is used as a discriminator. MessageType = 1 means it is a Message MessageType = 2 means it is a Comment MessageType = 3 AND and join to the SuperMessage means it is a SuperMessage The problem is that I cannot specify a condition on MessageType of the SuperMessage's Mapping

Entity Framework 4 / Linq: How to convert from DateTime to string in a query?

早过忘川 提交于 2020-01-09 07:15:16
问题 I have the following query: from a in Products select new ProductVM { id = a.id, modified = a.modified.ToString() } Which gives me an error of: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. The modified in the Products table is DateTime. The modified in the ProductVM class is string. Any ideas? This has to be a trivial issue. 回答1: ToString() is not supported in Linq to Entities - there is a list

Entity Framework 4 / Linq: How to convert from DateTime to string in a query?

别来无恙 提交于 2020-01-09 07:13:10
问题 I have the following query: from a in Products select new ProductVM { id = a.id, modified = a.modified.ToString() } Which gives me an error of: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. The modified in the Products table is DateTime. The modified in the ProductVM class is string. Any ideas? This has to be a trivial issue. 回答1: ToString() is not supported in Linq to Entities - there is a list