Casting in the Linq To SQL?

Try following public IQueryable fGetNewsTags(int id) { var result = from news in context.GetTable() where news. M_Id == id select (ITag)news; return result; }.

LINQ-to-SQL does not support multi-table inheritance (i.e. A single object with data from multiple tables). Entity Framework does, but is more complex; you use .

What you're doing is correct (you can't call Cast directly on the IQueryable ), but it's sub-optimal, because you're creating 2 lists when you really only need one. Use AsEnumerable instead of the first ToList : return items.AsEnumerable().Cast().ToList() Calling AsEnumerable will cause Cast to be evaluated on a IEnumerable not IQueryable so the cast will be done on the actual object returned from the DB, rather than directly in the DB.

What you're doing is correct (you can't call Cast directly on the IQueryable), but it's sub-optimal, because you're creating 2 lists when you really only need one. Use AsEnumerable instead of the first ToList : return items.AsEnumerable().Cast().ToList(); Calling AsEnumerable will cause Cast to be evaluated on a IEnumerable, not IQueryable, so the cast will be done on the actual object returned from the DB, rather than directly in the DB.

That's what I was hoping for, to avoid creating both lists. – Kelly Cline Jul 20 '10 at 18:58.

Alternately, you could do this instead: var items = from t in SomeTable where t. Condition == true select (SomeInterface) t; return items.ToList(); If t doesn't implement SomeInterface it will still fail at runtime, though. This will do the same thing, but won't fail; rather it will give you null if the object doesn't implement the interface: var items = from t in SomeTable where t.

Condition == true select t as SomeInterface; return items.ToList().

The question explains the first call to ToList, and there is good reason for it (although it should be AsEnumerable, not ToList). Calling Cast directly on the IQueryable would cause it to be executed on the DB, which can't work – Thomas Levesque Jul 20 '10 at 17:36.

A few solutions... Instead of converting data after collecting it, you could consider make a projection while collecting it. Return items. Select(x=>x as SomeInterface).ToList(); If you do this frequently, an extension method may still be helpful.It doesn't have to be Tolist/Cast/ToList as you mentioned though.

Cast will take an IEnumerable already, but you can create an overload that accepts an IQueryable, something like this (untested). Public IEnumerable Cast(this IQueryable input) { return input.AsEnumerable().Cast(); } This would whittle your code down to this: return items.Cast.ToList().

We were close! But since there is a cast involved, I needed a and a . Thanks!

– Kelly Cline Jul 20 '10 at 22:25 Yep, you're right. Edited the code to reflect this. – Phil Gilmore Jul 22 '10 at 22:06.

You may still be able to use them in your code, but they must be evaluated before the query is translated to Transact-SQL or after the results have been retrieved from the database. Provides a detailed matrix of mappings between CLR types and SQL Server types. Summarizes differences in behavior from the .

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions