在层之间传递对象时出现问题
我正在尝试做一些应该非常简单的事情,但我遇到了错误,我不知道如何纠正。另外,我什至不知道我是否以“正确”的方式做事。
我有 3 个实体。我的第一个实体叫比尔,它是我的主要实体。另外两个是重复和类型。 Bill 有外键(即 TypeId),它们指向各自的主键。 Type 和 Repeat 都很相似。他们有自己的 ID 和描述。
我正在尝试使用 EF 和 LINQ to Entity 执行以下操作:获取具有 Bill 的所有属性的所有帐单,但我想要 TypeDesc,而不是 TypeId。
这是一个包含 3 个项目的 N 层解决方案 到目前为止,我的数据层称为模型,业务层称为 BLL,演示文稿后来称为 GUI。
namespace BillApp.Model
{
public class BillModel
{
public List<BillType> GetAllBills()
{
using (BillsEntities be = new BillsEntities())
{
var results = from o in be.Bills
.Include("Type")
.Include("Repeat")
select new
{
BillId = o.BillId,
Name = o.Name,
URL = o.URL,
PaymentAmount = o.PaymentAmount,
Balance = o.Balance,
DueDate = o.DueDate,
TypeDesc = o.Type.TypeDesc,
RepeatDesc = o.Repeat.RepeatDesc,
Username = o.UserName
};
return results.ToList();
}
}
}
public class BillType
{
#region Properties
public int BillId { get; set; }
public string Name { get; set; }
public string URL { get; set; }
public decimal PaymentAmount { get; set; }
public decimal Balance { get; set; }
public DateTime DueDate { get; set; }
public string TypeDesc { get; set; }
public string RepeatDesc { get; set; }
public string Username { get; set; }
#endregion
}
}
结果返回错误无法将类型 System.Linq.IQueryable
隐式转换为 System.Collections.Generic.List
接下来我尝试并使用此代码将对象传递给我的 BLL。
namespace BillApp.BLL
{
public class BillBLL
{
public List<BillType> GetAllBills()
{
BillModel b = new BillModel();
return b.GetAllBills();
}
}
}
但是 BillType 有一个错误:找不到类型或命名空间名称“BillType”(您是否缺少 using 指令或程序集引用?)
我缺少什么?我能做什么更好?
I'm trying to do something that should be real simple but I'm getting errors I don't know how to correct. Also, I don't even know if I'm doing things the "correct" way.
I have 3 entities. My first entity is called Bill and it's my main one. The other two are Repeat and Type. Bill has foreign keys (i.e. TypeId) which point to their respective primary keys. Both Type and Repeat are similar. They have their Id and description.
What I'm trying to do with EF and LINQ to Entity: Get all bills with all the properties of Bill but instead of the TypeId, I want TypeDesc.
This is an N tier Solution with 3 projects
Thus far I have my data layer I call Model, business layer I call BLL and my presentation later I call GUI.
namespace BillApp.Model
{
public class BillModel
{
public List<BillType> GetAllBills()
{
using (BillsEntities be = new BillsEntities())
{
var results = from o in be.Bills
.Include("Type")
.Include("Repeat")
select new
{
BillId = o.BillId,
Name = o.Name,
URL = o.URL,
PaymentAmount = o.PaymentAmount,
Balance = o.Balance,
DueDate = o.DueDate,
TypeDesc = o.Type.TypeDesc,
RepeatDesc = o.Repeat.RepeatDesc,
Username = o.UserName
};
return results.ToList();
}
}
}
public class BillType
{
#region Properties
public int BillId { get; set; }
public string Name { get; set; }
public string URL { get; set; }
public decimal PaymentAmount { get; set; }
public decimal Balance { get; set; }
public DateTime DueDate { get; set; }
public string TypeDesc { get; set; }
public string RepeatDesc { get; set; }
public string Username { get; set; }
#endregion
}
}
results returns an error Cannot implicitly convert type System.Linq.IQueryable<AnonymousType#1>
to System.Collections.Generic.List<BillApp.Model.BillType>
Next I try and pass the object to my BLL with this code.
namespace BillApp.BLL
{
public class BillBLL
{
public List<BillType> GetAllBills()
{
BillModel b = new BillModel();
return b.GetAllBills();
}
}
}
But BillType has an error:The type or namespace name 'BillType' could not be found (are you missing a using directive or an assembly reference?)
What am I missing? What can I do better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为错误消息非常清楚:
results
的类型为IQueryable
并且您试图返回List
。要返回列表,您可以使用 ToList 方法。您还需要创建 BillType 的实例而不是匿名对象,以便它符合List
返回类型。您的 GetAllBills 方法如下所示:对于第二个错误,您可能只是缺少一个
using
指令来访问您的BillType
类型,该指令位于不同的命名空间中。你会在文件的顶部看到这一行:I think the error messages are pretty clear:
results
is of typeIQueryable<T>
and you're trying to return aList<T>
. To return a list instead, you can use the ToList method. You'll also need to create instances of BillType instead of anonymous objects, so that it conforms to theList<BillType>
return type. YourGetAllBills
method would look like this:For the second error you're probably just missing a
using
directive to have access to yourBillType
type which is in a different namespace. You'd have this line at the top of the file: