【问题标题】:how return an IEnumerable<> of my customized model?如何返回我的自定义模型的 IEnumerable<>?
【发布时间】:2015-01-01 08:17:50
【问题描述】:

我正在使用 EF6,我的模型名称之一是 tblAttachLabel。我已经定制了名为 AttachLabel 的模型。我需要我的自定义模型的 IEnumerable 填充 tblAttachLabel 模型。很容易退货

IEnumerable<tblAttachLabel> 

来自我的函数,但我需要返回

IEnumerable<AttachLabel>

所以我做这个代码:

public static IEnumerable<AttachLabel> GetAttachLabel()
    {            
        Entities db = new Entities();
        var x = from al in db.tblAttachLabels select al;

        List<AttachLabel> temp = new List<AttachLabel>();
        IEnumerable<AttachLabel> _attachLabel;
        foreach (var item in x)
        {
            AttachLabel a = new AttachLabel()
            {
                ID = item.ID,
                Text = item.Text
            };
            temp.Add(a);
        }
        _attachLabel = temp;

        return _attachLabel;
    }

但我知道当我使用 List 作为 temp 时,查询会执行,但我不想要这个。那么我怎样才能返回一个 IEnumerable 呢?

【问题讨论】:

  • but I know when I use a List for temp the query will execute 这是什么意思?你可以直接返回temp 应该没问题(因为List&lt;T&gt; 实现了IEnumerable&lt;T&gt;
  • 查询将在foreach 中执行。为什么不希望执行此查询?

标签: c# asp.net linq ienumerable


【解决方案1】:

试试这个:

public static IEnumerable<AttachLabel> GetAttachLabel()
{
    Entities db = new Entities();

    return from item in db.tblAttachLabels select new AttachLabel()
    {
        ID = item.ID,
        Text = item.Text
    };
}

【讨论】:

  • 你忘记了这里的 using 声明来处理上下文
【解决方案2】:
public static IEnumerable<AttachLabel> GetAttachLabel()
{
    Entities db = new Entities();
    var items = from al in db.tblAttachLabels select al;

    return items.Select(new AttachLabel()
    {
       ID = item.ID,
       Text = item.Text
    });

}

【讨论】:

    【解决方案3】:

    @haim770 的答案的另一种可能性和替代方法是带有 yield 关键字的循环:

    当您在语句中使用 yield 关键字时,表明它出现的方法、运算符或 get 访问器是迭代器。当您为自定义集合类型实现 IEnumerable 和 IEnumerator 模式时,使用 yield 定义迭代器无需显式的额外类(保存枚举状态的类,参见 IEnumerator 示例)。

    public static IEnumerable<AttachLabel> GetAttachLabel()
    {               
        using(Entities db = new Entities())
        {
            foreach (var item in db.tblAttachLabels)
            {
                AttachLabel a = new AttachLabel()
                {
                    ID = item.ID,
                    Text = item.Text
                };
                yield return a;
            }
        }
        yield break;
    }
    

    你的上下文也应该被释放,所以我添加了一个using 声明。

    并且不需要:

    from al in db.tblAttachLabels select al;
    

    因为它只返回与db.tblAttachLabels 相同的集合。

    【讨论】:

      【解决方案4】:

      其中之一,取决于您是否更喜欢 lambda 表达式:

      public static IEnumerable<AttachLabel> GetAttachLabel()
      {
          using (var db = new Entities())
          {
              return db.tblAttachLabels.Select(item => new AttachLabel
              {
                  ID = item.ID,
                  Text = item.Text
              });
          }
      }
      

      或不:

      public static IEnumerable<AttachLabel> GetAttachLabel()
      {
          using (var db = new Entities())
          {
              return from item in db.tblAttachLabels
                      select new AttachLabel
                      {
                          ID = item.ID,
                          Text = item.Text
                      };
          }
      }
      

      【讨论】:

      • 如果您有很多要映射的属性,请查看AutoMapper
      猜你喜欢
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多