Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<AnonymousType#2>' [duplicate]

StackOverflow https://stackoverflow.com/questions/23605252

Question

i 'm facing one compile time error like

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<AnonymousType#2>'

here is my code :

var query = Enumerable.Repeat(new
            {
                        Id = default(int),
                        Name = string.Empty,
                        type_id = default(int),
                        Ownername = string.Empty,
                        Ownermob = string.Empty,
                        ContactPerson = string.Empty,
                        ContactPersonmob = string.Empty,
                        Phone = string.Empty,
                        Mobile = string.Empty,
                        Room = string.Empty,
                        Build = string.Empty,
                        Road = string.Empty,
                        Area = string.Empty,
                        City = string.Empty,
                        country_id = default(int?),
                        state_id = default(int?),
                        Email = string.Empty,
                        Remark = string.Empty,
                        UserOFC = default(bool),
                        UserVAT = default(bool),
                        UserINV = default(bool),
                        UserNone = default(bool),
                        Username = string.Empty,
                        Register_Date = default(DateTime)},0).ToList();

                    query = db.Parties.Where(p => p.type_id.Equals(GroupByENTYPE)).OrderByDescending(p => p.Register_Date).Select(p => new
                    {
                        Id = p.Id,
                        Name = p.Name,
                        type_id = p.type_id,
                        Ownername = p.Ownername,
                        Ownermob = p.Ownermob,
                        ContactPerson = p.ContactPerson,
                        ContactPersonmob = p.ContactPersonmob,
                        Phone = p.Phone,
                        Mobile = p.Mobile,
                        Room = p.Room,
                        Build = p.Build,
                        Road = p.Road,
                        Area = p.Area,
                        City = p.City,
                        country_id = p.country_id,
                        state_id = p.state_id,
                        Email = p.Email,
                        Remark = p.Remark,
                        UserOFC = p.UserOFC,
                        UserVAT = p.UserVAT,
                        UserINV = p.UserINV,
                        UserNone = p.UserNone,
                        Username = db.Users.Where(u => u.Ref_no.Equals(p.User_id)).Select(u => u.Username).FirstOrDefault(),
                        Register_Date = p.Register_Date
                    }).FilterForColumn(ColumnName, SearchText).ToList();//here error occurs
                }

here for declaring common query part and assigning it at multiple condition. at FilterColumn part compile time error faced. how ever at another page same kind of this declaration not gives any problem.

here i put table schema that gives knowledge :

----------------------Updated----------------------------------------

Id  int Unchecked
Name    varchar(50) Unchecked
type_id int Unchecked
Ownername   varchar(50) Checked
Ownermob    nchar(10)   Checked
Room    varchar(10) Checked
Build   varchar(50) Checked
Road    text    Checked
Area    text    Checked
City    varchar(50) Checked
Phone   nchar(10)   Checked
Mobile  nchar(10)   Checked
Email   varchar(100)    Checked
ContactPerson   varchar(50) Checked
ContactPersonmob    nchar(10)   Checked
UserOFC bit Checked
UserVAT bit Checked
UserINV bit Checked
UserNone    bit Checked
state_id    int Checked
country_id  int Checked
Remark  text    Checked
Register_Date   smalldatetime   Unchecked
User_id char(14)    Unchecked

please help me...

Was it helpful?

Solution

It's hard to see from plain text. Try to use binary search to find incompatible properties. That is to

  1. comment out last half properties to see if incompatibilities happen in first half properties.
  2. If compiler still report errors, do the same for the first half properties.
  3. If compiler doesn't report errors, do the same for the last half properties.

OTHER TIPS

The properties of your anonymous types looks identical, so my best guess is that one of your default types differs from the actual types use when you have an object to use.

I think the best strategy for solving this would be to use the Null Object pattern


Resources:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top