Cannot implicitly convert type 'System.Collections.Generic.Listanonymous type' to 'System.Collections.Generic.Liststring'
You have to convert the anonymous object into a string representation.(Note I'm using C# 6.0 feature - string interpolation, you can replace it with string.Format in the previous versions.
Example:
return q.Select(x=>$"Name = {x.PDF.Name} File = {c.PDF.File}").ToList();
Cannot implicitly convert type 'System.Collections.Generic.Listanonymous type: string Access' to 'System.Collections.Generic.IEnumerablestring'
Just change this:
Select(x => new { x.Access })
To This:
Select(x => x.Access)
The reason is, new
makes your query to return an IEnumerable
of Anonymous types
, while you need an IEnumerable
of Strings
.
Cannot implicitly convert type 'System.Collections.Generic.Listanonymous type
I'd expect your TaskSchedule to look like:
public class TaskSchedule
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public User user { get; set; }
public int userId { get; set; }
public List<Note> Notes { get; set; }
}
(The Note class would still have a single TaskSchedule property - EF can infer that TS:N is 1:M as a result because one end of the rel is a single property and the other end is a collection)
And to get task schedules and their notes you could e.g. say:
public async Task<IList<TaskSchedule>> GetTaskSchedulesByUser(int UserId)
{
var userTaskSchedule = await _context.TaskSchedules
.Include(ts => ts.Notes)
.Where(u => u.schedule.userId == UserId)
.ToListAsync();
return userTaskSchedule;
}
Bucketloads of info on how to configure relationships and also how to query related data
Cannot implicitly convert type Listanonymous type: .. to IEnumerableProduct
Its seems that your expected return output is combination of both Product
and ProductType
model class. So make a DTO (Data Transfer Object) class as follows which will contain all the fields of your output query.
public class ProductDto
{
public long ProductId { get; set; }
public string Number { get; set; }
public double Amount { get; set; }
public double PrimeCostEUR { get; set; }
public string ProductTypeName { get; set; }
}
Now write your controller method as follows where return type of the method is IEnumerable<ProductDto>
:
public async Task<IEnumerable<ProductDto>> TestProducts()
{
var items = await _context.Products.Select(p => new ProductDto
{
ProductId= p.ProductId,
Number= p.Number,
Amount= p.Amount,
PrimeCostEUR= p.PrimeCostEUR,
ProductTypeName = p.ProductType.NameType
}).ToListAsync();
return items;
}
Moreover you don't need explicit LINQ join as you did, you can achieve the same thing using EF projection as I have done with more simplicity.
Cannot implicitly convert type 'System.Collections.Generic.IEnumerableAnonymous
Type#1' to 'System.Collections.Generic.ListmodelClass
List of anonymous types cannot be casted to list of transactions. Looks like your Transaction
class do not have AccountNumber
property. Also you cannot return anonymous objects from methods. So you should create some type which will hold required data:
public class AccountTransaction
{
public int LocalAmount { get; set; }
public int AccountNumber { get; set; }
}
And return these objects:
public static IEnumerable<AccountTransaction> GetAllTransactions()
{
using (var context = new CostReportEntities())
{
return (from t in context.Transactions
join acc in context.Accounts
on t.AccountID equals acc.AccountID
select new AccountTransaction {
AccountNumber = acc.AccountNumber,
LocalAmount = t.LocalAmount
}).ToList();
}
}
BTW you don't need duplicate join condition in where filter
Cannot implicitly convert type 'System.Collections.Generic.Liststring' to 'System.Collections.Generic.IEnumerableLoginForm.
UserInfo_Result'
It's because your db.UserInfo()
method returns collection of string
and ToString()
makes it List<string>
, but method GetUserInfo()
returns IEnumerable<UserInfo_Result>
.
You should either change your db.UserInfo()
to return IEnumerable<UserInfo_Result>
, or construct IEnumerable<UserInfo_Result>
before returning (at GetUserInfo
), or just change return type of GetUserInfo
to List<string>
. It dependes on what you need.
Why doesn't IEnumerable of anonymous types return a Listobject on ToList()?
If
List<<anonymous type: string FirstName>>
cannot be converted toList<object>
, then why can it be converted toIEnumerable<object>
?
That's because IEnumerable<T>
is covariant and List<T>
is not. It has nothing to do with anonymous types.
If the code you wrote was to work you'd be able to use List<string>
as List<object>
and add anything to it breaking type safety.
You can make your code work by passing a generic type parameter to ToList
call:
static List<object> GetAnonList(IEnumerable<string> names)
{
return names.Select(name => new { FirstName = name }).ToList<object>();
}
But there is very little you can do with that outside of this method. You won't be able to access FirstName
property unless you use reflection.
Cannot implicitly convert type 'System.Collections.Generic.Listanonymous type: System.DateTime timeStamp, double finalState, double consuption'
Total_PowerList variable type is declared as List<Total_Power> so you cannot assign a different type afterwards. Instead, just create a new variable and pass it in partial view.
var KwhList = Total_PowerList.GroupBy(
i => i.DeviceTimeStamp.Date,
(timeStamp, dayilyData) => new { timeStamp, dayilyData })
.Select(i => new
{
i.timeStamp,
finalState = i.dayilyData.Max(x => x.KWH),
consuption = (i.dayilyData.Max(x => x.KWH) - i.dayilyData.Min(x => x.KWH))
}).ToList();
Related Topics
C# MACro Definitions in Preprocessor
Update App.Config System.Net Setting at Runtime
Displaying Tooltip on Mouse Hover of a Text
Wpf Binding Not Updating the View
How to Translate a List<String> into a SQLparameter for a SQL in Statement
How to Terminate a Thread in C#
How to Log All Thrown Exceptions
How to Generate a 3-D Surface from Isolines
How to Add My New User Control to the Toolbox or a New Winform
.Net: Unable to Cast Object to Interface It Implements
Xmlserialize a Custom Collection with an Attribute
Visualizing an Ast Created with Antlr (In a .Net Environment)
Deserializing JSON Array into Strongly Typed .Net Object
ASP.NET C# Static Variables Are Global
C# Structs: Unassigned Local Variable
C# - Inconsistent Math Operation Result on 32-Bit and 64-Bit