score:2
Accepted answer
You should integrate your foreach
into LINQ query:
var query = from item in weatherData.Descendants("Observation")
group item by (string)item.Element("Station") into g
let maxDate = g.Max(o => (DateTime)o.Element("DateTime"))
let latestObservation = g.FirstOrDefault(o => (DateTime)o.Element("DateTime") == maxDate)
select new Station()
{
Name = g.Key,
MostRecentDate = maxDate,
LastTemperature = (decimal)latestObservation.Element("Temperature")
}
But it will give you IEnumerable<Station>
not IQueryable<>
. You can get IQueryable<Station>
by calling AsQueryable()
method, but that really doesn't make any sense here.
Also, you can cast XElement
to DateTime
and Decimal
directly, instead of casting to string
first and then calling Parse
method.
Source: stackoverflow.com
Related Query
- Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.MemberExpression'
- Unable to cast object of type WhereSelectListIterator 2 System.Collections.Generic.List
- Unable to cast object of type 'System.Data.Linq.DataQuery`1[System.Int32]' to type 'System.IConvertible'
- Unable to cast object of type 'WhereEnumerableIterator`1' to type 'System.Collections.Generic.ICollection`1
- Unable to cast object of type 'System.Collections.Generic.List`1[System.Decimal]' to type 'System.IConvertible'
- Unable to cast object of type 'System.Int32' to type 'System.String'
- Unable to cast a object of type 'SZArrayEnumerator' to type 'System.Collections.Generic.IEnumerator' 1[System.String]'
- Unable to cast object of type WhereSelectListIterator
- Unable to cast object of type 'System.Linq.EnumerableQuery to type 'Microsoft.Azure.Documents.Linq.IDocumentQuery
- Unable to cast object of type System.Func`2 with Int64
- Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[]' using linq lambda expression
- Unable to cast object of type '<>f__AnonymousType5`6
- Linq cast conversion Xelement error: Unable to cast object of type 'System.Xml.Linq.XElement' to type 'System.IConvertible'
- Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.Data.IDataReader'
- Unable to cast object of type 'WhereSelectListIterator` in LINQ
- Unable to cast object of type 'System.Int16' to type 'System.String'
- Unable to cast object of type 'System.Data.Objects.ObjectQuery`1 [Manager.Data.Channel]' to type 'Manager.Data.Channel'
- Unable to cast object of type 'WhereSelectEnumerableIterator
- LINQ Issue: Unable to cast object of type 'System.Reflection.Module' to type 'System.Reflection.Emit.ModuleBuilder
- Dapper unable to cast object of type 'Microsoft.SqlServer.Types.SqlGeography' to type 'System.Data.Entity.Spatial.DbGeography'
- Unable to cast object of type 'System.Linq.Expressions.FieldExpression' to type 'System.Linq.Expressions.ParameterExpression
- Removing duplicates using LINQ returns "Error removing duplicates: Unable to cast object of type 'System.Int32' to type 'System.String'."
- Unable to cast object of type 'System.Decimal' to type 'System.String'
- Unable to cast object of type 'System.Linq.Expressions.MethodCallExpression' to type 'System.Linq.Expressions.LambdaExpression'
- LINQ with String.Join() error: "System.InvalidCastException: Unable to cast object of type '<DistinctIterator>
- Unable to cast object of type 'System.Data.Objects.ObjectQuery`1 ..... to type 'DatabaseModel1......'. ASP
- Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`
- Unable to cast object of type 'System.Data.EnumerableRowCollection`1[System.Int32]' to type 'System.IConvertible'
- C# - Unable to cast object of type 'System.Linq.Expressions.ConstantExpression' to type 'System.Linq.Expressions.LambdaExpression'
- Unable to cast object of type 'SingleResult'
More Query from same tag
- nHibernate join and take one record
- Wrap text in xml into element tags in Linq to Xml
- Grouping on DataTable using LINQ
- only name in dropdown
- Pivot column entries into a comma separated list in a single row for distinct adjacent column?
- Add the where clause dynamically in Entity Framework
- Linq Execute query InvalidOperationException
- Grouping data based on date entity framework and LINQ
- LINQ to entities does not recognize the method 'System.DecimalToDecimal
- Linq Grouping query
- Date range falling between two dates in a LINQ query
- How can I use Linq instead of foreach to get items out of a nested set?
- Linq Queries abstraction with expressions, which way is better?
- Linq returning 1 record
- How to check if an expression is empty (void)?
- How to deal with a System.InvalidOperationExcpetion in Dictionary's TryGetValue method
- Why does LINQ-to-SQL generate this SQL statement for a comparison
- Left excluding join using LINQ in vb.net
- Linq List Ranking in C#
- Recurrence of events (LINQ query)
- How are these two Guids different?
- SubSonic Outer Join
- How to remove duplicates in a list while keeping elements with highest scores among duplicates, in C# with Linq
- LINQ query for Group by a key and then order by different column
- Reset(not removing) property for duplicates in a list using linq
- Coverting a foreach loop containing an if statement into linq method syntax in C#
- Left Trim in Linq
- Nested group by based on flatten record set
- LINQ Query for Unsanitized Data
- how to use a method with 2 parameters in my lambda expression