开发者

Having issues passing Objects among tiers

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?


I think the error messages are pretty clear: results is of type IQueryable<T> and you're trying to return a List<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 the List<BillType> return type. Your GetAllBills method would look like this:

public List<BillType> GetAllBills()
{
    using (BillsEntities be = new BillsEntities())
    {
        var results = from o in be.Bills
                     .Include("Type")
                     .Include("Repeat")
                      select new BillType
                      {
                          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();
    }
}

For the second error you're probably just missing a using directive to have access to your BillType type which is in a different namespace. You'd have this line at the top of the file:

using BillApp.Model;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜