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 Articles
- 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'
- Does Linq Contains() check for HashSet?
- Specific elements from XML string to a datagridview or datatable (C#)
- LINQ lookup if table column contains value and return list of objects
- LINQ result on datatable without for loop
- Linq Update taking long time to process
- Linq query - use string as property?
- How to do "let" in dynamic LINQ?
- Found Duplicate record in excel sheet using c# .net
- How to output a relational object using Linq on View models in Entity Framework Core
- LINQ query - search in SQL Server database
- Xml linq query based on 2 Elements
- How do I print different names from a list?
- How to check list has all array items?
- Using LINQ to query huge CSV and Excel
- Find the category which has 3 or more than 3 products data by using the linq and lambda query
- Why are these linq outputs different?
- Linq query from list containing check for null condition
- searching using linq
- Writing a SQL query with LINQ
- Build a linq query based on specific criteria c#