Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, June 2, 2013 1:08 PM
return(frompro in_db.PropertyModels
selectnewPropertyEntity()
{
DealType = pro.DealType,
Department = pro.DealType,
Id = pro.Id,
ProjectName = pro.ProjectName,
PropertyPosts = (
frompos inpro.PropertyPosts selectnewPropertyPostEntity() { Id = pos.Id, PostType = pos.PostType, PropertyModel_Id = pos.PropertyModel_Id}).ToList(),
PropertyType = pro.PropertyType
}).ToList();
Hello,
I have Class of reference second class like this
public partial class PropertyModel
{
public PropertyModel()
{
this.PropertyPosts = new HashSet<PropertyPost>();
}
public int Id { get; set; }
public string PropertyType { get; set; }
public string DealType { get; set; }
public string Department { get; set; }
public string ProjectName { get; set; }
public virtual ICollection<PropertyPost> PropertyPosts { get; set; }
}
this class has second Property post class like this
public partial class PropertyPost
{
public int Id { get; set; }
public string PostType { get; set; }
public Nullable<int> PropertyModel_Id { get; set; }
public virtual PropertyModel PropertyModel { get; set; }
}
Ans I am using this class into WCF services this sevice is generated by EntityFramework and I had created another class like same what I have without partial
now my method is above is will give me error like this :
Error 1 Cannot implicitly convert type 'System.Collections.Generic.ICollection<BuilderTrackerServer.PropertyPost>' to 'System.Collections.Generic.ICollection<BuilderTrackerServer.PropertyPostEntity>'. An explicit conversion exists (are you missing a cast?) D:\Live Projects\BuilderTracker\BuilderTracker\BuilderTrackerServer\BuilderTrackerServices.cs 26 51 BuilderTrackerServer
how I can resolved this problem.?
All replies (11)
Sunday, June 2, 2013 3:10 PM ✅Answered
Hi,
In your first (badly formatted) code snippet you are basically mapping public properties between the two types PropertyModel and PropertyPostEntity in order to return a collection of PropertyPostEntity, so the question in the title of this posting is already answered by you. If there's no conversion between types, you can map their properties.
Details: The first type has a property named PropertyModel.PropertyPosts returning a ICollection<PropertyModel.PropertyPost>. You are assigning an instance of this ICollection<PropertyModel.PropertyPost> to the property PropertyPostEntity.PropertyPosts which is of type ICollection<PropertyPostEntity>. Since no implicit conversion is possible between the two, the C# compiler errors out telling you to try an explicit conversion (for the case when a TypeConverter is implemented).
Here's my technical reconstruction of the issue you're facing (look at the line with the "wrong"-comment):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication15
{
class Program
{
static void Main(string[] args)
{
new Program().Test();
Console.ReadLine();
}
List<PropertyModel> PropertyModels;
public void Test()
{
var propertyPosts = new HashSet<PropertyPost>();
propertyPosts.Add(new PropertyPost { Id=1, PostType="T"});
PropertyModels = new List<PropertyModel>
{
new PropertyModel { DealType="D", Department = "A", Id = 101, ProjectName="Project 1", PropertyType = "TYPE1", PropertyPosts = propertyPosts},
new PropertyModel { DealType="E", Department = "A", Id = 102, ProjectName="Project 2", PropertyType = "TYPE2", PropertyPosts = propertyPosts },
new PropertyModel { DealType="D", Department = "B", Id = 103, ProjectName="Project 1", PropertyType = "TYPE2", PropertyPosts = propertyPosts },
new PropertyModel { DealType="F", Department = "B", Id = 104, ProjectName="Project 3", PropertyType = "TYPE1", PropertyPosts = propertyPosts },
};
var result = GetPropertyEntityList();
}
public ICollection<PropertyEntity> GetPropertyEntityList()
{
var result = (from pro in PropertyModels select new PropertyEntity()
{
DealType = pro.DealType,
Department = pro.DealType,
Id = pro.Id,
ProjectName = pro.ProjectName,
PropertyPosts = (from pos in pro.PropertyPosts select new PropertyPostEntity()
{
Id = pos.Id,
PostType = pos.PostType,
PropertyModel_Id = pos.PropertyModel_Id
}).ToList(),
PropertyType = pro.PropertyType
}).ToList();
return result;
}
}
public partial class PropertyModel
{
public PropertyModel()
{
this.PropertyPosts = new HashSet<PropertyPost>();
}
public int Id { get; set; }
public string PropertyType { get; set; }
public string DealType { get; set; }
public string Department { get; set; }
public string ProjectName { get; set; }
public virtual ICollection<PropertyPost> PropertyPosts { get; set; }
}
public partial class PropertyPost
{
public int Id { get; set; }
public string PostType { get; set; }
public Nullable<int> PropertyModel_Id { get; set; }
public virtual PropertyModel PropertyModel { get; set; }
}
class PropertyEntity
{
public string DealType {get; set;}
public string Department {get; set;}
public int Id {get; set;}
public string ProjectName {get; set;}
public string PropertyType { get; set; }
// wrong: public ICollection<PropertyPost> PropertyPosts {get; set;}
// correct:
public ICollection<PropertyPostEntity> PropertyPosts {get; set;}
}
class PropertyPostEntity
{
public int Id { get; set; }
public string PostType { get; set; }
public int? PropertyModel_Id { get; set; }
public int PropertyType { get; set; }
}
}
P.S. For the future: please provide formatted and reproducible code.
Marcel
Sunday, June 2, 2013 4:11 PM ✅Answered
On 6/2/2013 9:08 AM, Jitendra Jadav wrote:
Ans I am using this class into WCF services this sevice is generated by EntityFramework and I had created another class like same what I have without partial
EF doesn't make WCF services. A WCF project type makes WCF services and the clients.
now my method is above is will give me error like this :
Error 1 Cannot implicitly convert type 'System.Collections.Generic.ICollection<BuilderTrackerServer.PropertyPost>' to 'System.Collections.Generic.ICollection<BuilderTrackerServer.PropertyPostEntity>'. An explicit conversion exists (are you missing a cast?) D:\Live Projects\BuilderTracker\BuilderTracker\BuilderTrackerServer\BuilderTrackerServices.cs 26 51 BuilderTrackerServer
No, you can't convert the object from one object type to another object type.
1) The objects must be in the same namespace.
2) The objects must have the same object name
The above must be true in order for you to make the copy without doing an explicit cast, which you cannot do in this situation.
The only thing you can do is do an AddRange(), which allows you to read an object out of one collection, create a new object for the other collection and copy the property data from one object to the other, loading the new object into its collection and while doing it on the fly.
Below, the code is getting a List<T> of object back from the Business Logic Layer that's sitting behind a WCF service. The List of DTO(s) coming back were derived in another namespace the namespace BLL which had to be copied to a List<T> in another namespace.
public List<DTOAuthor> GetAuthors()
{
var client = new WCFServiceReference.Service1Client();
var authors = new List<DTOAuthor>();
try
{
var dtoauthors = client.GetAuthors();
client.Close();
authors.AddRange(dtoauthors.Select(dtoauthor => new DTOAuthor()
{
AuthorID = dtoauthor.AuthorID,
FirstName = dtoauthor.FirstName,
LastName = dtoauthor.LastName,
IsUpdate = dtoauthor.IsUpdate
}).ToList());
}
catch (Exception ex)
{
client.Abort();
}
}
The above was being done with List<T>'s. I don't know if you can do the same with an ICollection.
Sunday, June 2, 2013 4:29 PM
PropertyModel contains a property PropertyPosts with type ICollection<PropertyPost>, but you are assigning PropertyPostEntity in the inner from.
Muthukrishnan Ramasamy
net4.rmkrishnan.net
Use only what you need, Reduce global warming
Sunday, June 2, 2013 4:39 PM
Hi darnold924,
No, you can't convert the object from one object type to another object type.
That's not quite correct. One of the objects may have a TypeConverterAttribut pointing to a TypeConverter that does the conversion between the otherwise unrelated types. The compiler error mentioned above actually includes this possibility.
Marcel
Sunday, June 2, 2013 4:45 PM
Hi Muthukrishnan Ramasamy,
PropertyPosts in the first snippet belongs actually to a PropertyEntity object, not to PropertyModel. He is mapping PropertyModel.PropertyPosts to PropertyEntity.PropertyPosts, and he does it quite well by assigning a ICollection<PropertyPostEntity> to PropertyEntity.PropertyPosts. But this implies that PropertyEntity.PropertyPosts is also of type ICollection<PropertyPostEntity> and not of type ICollection<PropertyPost>.
Marcel
Sunday, June 2, 2013 4:51 PM
Can you please post the detail about PropertyEntity
Muthukrishnan Ramasamy
net4.rmkrishnan.net
Use only what you need, Reduce global warming
Sunday, June 2, 2013 5:23 PM
Ramasamy,
in the first snippet you already have almost all you need to know:
return
(from pro in PropertyModels
select new PropertyEntity()
{
DealType = pro.DealType,
Department = pro.DealType,
Id = pro.Id,
ProjectName = pro.ProjectName,
PropertyPosts = (from pos in pro.PropertyPosts
select new PropertyPostEntity()
{
Id = pos.Id,
PostType = pos.PostType,
PropertyModel_Id = pos.PropertyModel_Id
}).ToList(),
PropertyType = pro.PropertyType
}
).ToList();
The only way to reproduce the aformentioned compilation error is to assign the results of the inner query to a property that does not accept a ICollection<PropertyPostEntity>.
Marcel
Sunday, June 2, 2013 5:29 PM
I need you to check the type of PropertyPosts property in PropertyEntity
Muthukrishnan Ramasamy
net4.rmkrishnan.net
Use only what you need, Reduce global warming
Sunday, June 2, 2013 5:42 PM
It's not my posting, so I suppose you actually mean Jitendra Jadav. But why do you need this information when you already know what's getting assigned?
Marcel
Monday, June 3, 2013 6:46 AM
Hi Cor,
the only property name starting with an uppercase I is ID. And if the O.P. really does WCF there is already a contract interface for him to use. Could you please elaborate on your suggestions?
Marcel
Monday, June 3, 2013 7:11 AM
Hi Cor,
the only property name starting with an uppercase I is ID. And if the O.P. really does WCF there is already a contract interface for him to use. Could you please elaborate on your suggestions?
Marcel
Marcel,
You are right, removed my reply.
Success
Cor