【发布时间】:2015-03-24 06:55:42
【问题描述】:
我的旧程序使用 3 层 用户界面、服务、业务
现在我想添加数据层并使用实体框架
商业:
public abstract class ITypeCollection
{
public int Id ;
public string name;
public int subset;
}
和
public class TTag:ITypeCollection
{
}
并使用存储库模式:
public interface ITypeRepository
{
IList<TTag> FindAllTTag();
}
我实现了 ITypeRepository
公共类 TypeRepository : ITypeRepository {
public IList<TTag> FindAllTTag()
{
using(var dbContext = new PAMEntities())
{
List<TypeCollectionSet> res = (from c in dbContext.TypeCollectionSets
join t in dbContext.TypeCollectionSet_TTag
on c.Id equals t.Id
select c).ToList();
return (TTag) res;
}
}
但是返回(TTag)res时出错;
错误:
错误 1 无法将类型“System.Collections.Generic.List”转换为“PAM.Model.TTag”
我的 TypeCollectionSet :
public partial class TypeCollectionSet
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public TypeCollectionSet()
{
this.TaggedSet = new HashSet<TaggedSet>();
this.TransactionSet = new HashSet<TransactionSet>();
this.AccountSets = new HashSet<AccountSet>();
}
public int Id { get; set; }
public string Name { get; set; }
public int Subset { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TaggedSet> TaggedSet { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TransactionSet> TransactionSet { get; set; }
public virtual TypeCollectionSet_TAccount TypeCollectionSet_TAccount { get; set; }
public virtual TypeCollectionSet_TExpense TypeCollectionSet_TExpense { get; set; }
public virtual TypeCollectionSet_TIncome TypeCollectionSet_TIncome { get; set; }
public virtual TypeCollectionSet_TTag TypeCollectionSet_TTag { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AccountSet> AccountSets { get; set; }
}
【问题讨论】:
-
您遇到了错误,因为这不是您声明为该方法的返回类型的类型?
-
@RowlandShaw 好的,我有两个类,它们具有剪切属性。标识,名称,子集。我想从使用实体框架创建的课程转换为我的商务课程
-
您正在尝试将列表本身转换为 TTag。使用 LINQ 转换列表中的所有元素,然后返回列表
-
@thakrage 我是初学者,你能举个例子吗,我没注意到你的意思
-
我现在已经发布了答案
标签: c# casting repository-pattern