Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doing a Cast Within a LINQ Query

Is it possible to do a cast within a LINQ query (for the compiler's sake)?

The following code isn't terrible, but it would be nice to make it into one query:

Content content = dataStore.RootControl as Controls.Content;  List<TabSection> tabList = (from t in content.ChildControls                             select t).OfType<TabSection>().ToList();  List<Paragraph> paragraphList = (from t in tabList                                  from p in t.ChildControls                                  select p).OfType<Paragraph>().ToList();  List<Line> parentLineList = (from p in paragraphList                              from pl in p.ChildControls                              select pl).OfType<Line>().ToList(); 

The code continues on with a few more queries, but the gist is I have to create a List out of each query in order for the compiler to know that all of the objects in content.ChildControls are of type TabSection and all of the objects in t.ChildControls are of type Paragraph...and so on and and so forth.

Is there a way within the LINQ query to tell the compiler that t in from t in content.ChildControls is a TabSection?

like image 697
Bob Wintemberg Avatar asked Oct 01 '08 16:10

Bob Wintemberg


People also ask

How cast the LINQ query in the C#?

LINQ Cast() Method In LINQ, Cast operator is used to cast/convert all the elements present in a collection into a specified data type of new collection. In case if we try to cast/convert different types of elements (string/integer) in the collection, then the conversion will fail, and it will throw an exception.

What is the cast operator in LINQ?

The Cast operator attempts to convert all the items within a collection to another type and return them in a new collection. If an item fails to convert then an exception is thrown. This cast method or operator uses deferred execution.

Can I join a table to a list using LINQ?

When the query actually runs, LINQ should be able to create a temp table or table variable with the data from the local list and then join on that. This is a feature that should absolutely be included in the framework.


2 Answers

Depending on what you are trying to do, one of these might do the trick:

List<Line> parentLineList1 =   (from t in content.ChildControls.OfType<TabSection>()    from p in t.ChildControls.OfType<Paragraph>()    from pl in p.ChildControls.OfType<Line>()    select pl).ToList();  List<Line> parentLineList2 =   (from TabSection t in content.ChildControls    from Paragraph p in t.ChildControls    from Line pl in p.ChildControls    select pl).ToList(); 

Note that one uses OfType<T>(), which you were using. This will filter the results and return only the items of the specified type. The second query implicitly uses Cast<T>(), which casts the results into the specified type. If any item cannot be cast, an exception is thrown. As mentioned by Turbulent Intellect, you should refrain from calling ToList() as long as possible, or try to avoid it altogether.

like image 31
Lucas Avatar answered Oct 06 '22 19:10

Lucas


Try this:

from TabSection t in content.ChildControls 

Also, even if this were not available (or for a different, future scenario you may encounter), you wouldn't be restricted to converting everything to Lists. Converting to a List causes query evaluation on the spot. But if you removing the ToList call, you could work with the IEnumerable type, which would continue to defer the execution of the query until you actually iterate or store in a real container.

like image 193
Chris Ammerman Avatar answered Oct 06 '22 19:10

Chris Ammerman