簡體   English   中英

如何在C#3分層體系結構中將輸出列表分配給ListView

[英]How to assign a output list to ListView in c# 3 layered architecture

我正在使用EF Code First模塊開發一個具有三層體系結構的應用程序。

  1. UI層(UI)
  2. 業務邏輯層(BLL)
  3. 數據訪問層(DAL)

我使用LINQ從DAL中的數據庫中檢索數據,並將其傳遞給BLL以及從BLL傳遞給UI

例如

在DAL中

    public object ReadUsers()
    {
        var users = db.Users.Select(u => new { u.UserName, u.IsDisable, u.FullName, u.Descriprion }).ToList();
        return users;
    }

在BLL中

    public object ReadUsers()
    {
        return _userDAL.ReadUsers();
    }

並在UI中-將用戶列表分配給ListView

        var users = _userBLL.ReadUsers();
        users.ToList()
            .ForEach(x => lvUserRoleGroup.Items.Add(
                new ListViewItem(
                    new string[] { x.UserName, x.IsDisable.ToString(), x.FullName, x.Descriprion }))
                );

但是它拋出一個錯誤

錯誤1'對象'不包含'ToList'的定義,找不到擴展方法'ToList'接受類型為'object'的第一個參數(是否缺少using指令或程序集引用?)

誰能告訴我如何在ListView顯示輸出? 可以使用DataGridView正常工作,如下所示-

        var users = _userBLL.ReadUsers();
        dataGridView1.DataSource = users;

當您返回users從你的方法,它轉換object ,當你調用ToList()方法,以.so object的引用有沒有那么method.You必須將它轉換回你的對象的type.And因為你有匿名類型,您需要使用字段x.UserName, x.IsDisable.ToString(), x.FullName, x.Descriprion和其他任何地方(而不是匿名類型 x.UserName, x.IsDisable.ToString(), x.FullName, x.Descriprion創建自定義類,然后使用您的自定義類型 ,然后將其List<CustomClass>List<CustomClass>

或像這樣更改objects

這將是你的課

class CustomClass
{
   public string UserName {get ;set ;}
   public bool IsDisable  {get ;set ;}
   public string FullName {get ;set ;}
   public string Description {get ;set ;}
}

在DAL中

public List<CustomClass> ReadUsers()
{
        var users = db.Users.Select(u => new CustomClass{ UserName = u.UserName, IsDisable = u.IsDisable, FullName = u.FullName, Description u.Descriprion }).ToList();
        return users;
}

在BLL中

public List<CustomClass> ReadUsers()
{
    return _userDAL.ReadUsers();
}

並在UI中-將用戶列表分配給ListView

List<CustomClass> users = _userBLL.ReadUsers();

            users.ForEach(x => lvUserRoleGroup.Items.Add(
                new ListViewItem(
                    new string[] { x.UserName, x.IsDisable.ToString(), x.FullName, x.Descriprion }))
                );

將對System.Linq的引用添加到失敗的項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM