TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
L A
NA
170
171.5k
Can't convert List<AnonymousType#1> to List<Model>
Jun 24 2017 12:23 PM
Hello all,
In my ASP.NETMVC application have 4 model classes.
ProductModel
public
class
ProductModel
{
public
int
ProductID {
get
;
set
; }
public
string
ProductName {
get
;
set
; }
public
int
SupplierID {
get
;
set
; }
public
int
CategoryID {
get
;
set
; }
}
CategoriesModel
public
class
CategoriesModel
{
public
int
CategoryID {
get
;
set
; }
public
string
CategoryName {
get
;
set
; }
public
string
Description {
get
;
set
; }
}
SuppliersModel
public
class
SuppliersModel
{
public
int
SupplierID {
get
;
set
; }
public
string
CompanyName {
get
;
set
; }
public
string
ContactName {
get
;
set
; }
public
string
ContactTitle {
get
;
set
; }
public
string
Phone {
get
;
set
; }
public
string
City {
get
;
set
; }
public
string
Country {
get
;
set
; }
}
Products_Categories_SuppliersModel - Class with all the required properties from above three classes.
public
class
Products_Categories_SuppliersModel
{
public
string
ProductName {
get
;
set
; }
public
string
ContactName {
get
;
set
; }
public
string
ContactTitle {
get
;
set
; }
public
string
CompanyName {
get
;
set
; }
public
string
Phone {
get
;
set
; }
public
string
City {
get
;
set
; }
public
string
Country {
get
;
set
; }
public
string
CategoryName {
get
;
set
; }
public
string
Description {
get
;
set
; }
}
I have data in List<ProductModel>, List<CategoriesModel>, List<SuppliersModel>. Used LINQ to join all three Lists<>.
public
List<Products_Categories_SuppliersModel> GetProduct_Category_SupplierData()
{
try
{
List<ProductModel> prodData = GetProductsData().ToList();
List<CategoriesModel> categData = GetCategoriesData().ToList();
List<SuppliersModel> supplData = GetSuppliersData();
var DBData = (from p
in
prodData
join c
in
categData
on p.CategoryID equals c.CategoryID
join s
in
supplData on p.SupplierID equals s.SupplierID
select
new
{
p.ProductName,
c.CategoryName,
c.Description,
s.ContactName,
s.ContactTitle,
s.CompanyName,
s.Phone,
s.City,
s.Country
});
return
DBData.ToList();
}
catch
(Exception) {
return
null
; }
finally
{}
}
But Line # 24 throws error - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<MVCApp.Models.Products_Categories_SuppliersModel>'.
What's the mistake in above code.? I have created Products_Categories_SuppliersModel class such that return data should be of 'Products_Categories_SuppliersModel' type.
Please suggest me to figure out my mistakes & help me code in a better way.
Thanks in advance.
Reply
Answers (
5
)
Getting context in a Page (Android/Xamarin)
Whit is the different between Task vs Thread?