简体   繁体   English

获取IEnumerable的元素

[英]Get elements of an IEnumerable

I am getting a list of objects like this: 我得到这样的对象列表:

IEnumerable personeller = (IEnumerable)sicil_model.OlayForm_Personel();

I am looping inside it like this 我在里面这样循环

foreach (var personel in personeller)
{
 //can't do anything...
}

I am looping inside personeller and I can see all elements like this 我在personeller内部循环,我可以看到所有这样的元素

personeller[0] | { Id = 5, Kimlik = "Koray Durudoğan", EMail = "koray@outlook.com", DepartmanAdi = "Yazilim", PozisyonAdi = "Yazilim Geliştirme" }

personeller[1] | { Id = 6, Kimlik = "Hasan Pınar", EMail = "hasan@dincerelektronik.com.tr", DepartmanAdi = "Yazilim", PozisyonAdi = "Yazilim Geliştirme" }
    .
    .
    .

And goes like this.. The thing is, I can't access elements of objects. 就像这样。事情是,我无法访问对象的元素。 For example, how can I get Id of personeller[0] or Kimlik of personeller[1] ? 例如,如何获取personeller [0]的ID或personeller [1]的Kimlik?

I made a class like this 我上了这样的课

public class Personel_Form 
{
    public int Id { get; set; }
    public string Kimlik { get; set; }
    public string EMail { get; set; }
    public string DepartmanAdi { get; set; }
    public string PozisyonAdi { get; set; }
}

but I couldn't manage to convert 'personel' to this class, inside my foreach loop. 但是我无法在foreach循环中将“ personel”转换为此类。

Lastly, Here is the method which turns the list: 最后,这是翻转列表的方法:

public object OlayForm_Personel() 
    {
        return (from p in Entity.GetEntity().Sicil
                           select new 
                           {
                            Id = p.Id,
                            Kimlik = p.Isim + " " + p.Soyad,
                            EMail = p.EMail,
                            DepartmanAdi = p.Departman.DepartmanAdi,
                            PozisyonAdi = p.Pozisyon1.PozisyonAdi
                           }).ToList();
    }

From your sentence of I am looping inside personeller and I can see all elements like this and the given sample data I deduce that the collection returned by the function stores items of the Personel_Form type. 从您的语句中, 我进入了Personeller内部,可以看到所有类似的元素以及给定的示例数据,我推断出该函数返回的集合存储了Personel_Form类型的项目。

Two options: 两种选择:

  1. You are casting it into the non genetic IEnumerable that holds the items as object s. 您正在将其转换为将项目作为object s的非遗传IEnumerable you can cast it to the correct type: 您可以将其转换为正确的类型:

     IEnumerable personeller = (IEnumerable)sicil_model.OlayForm_Personel(); foreach (var personel in personeller) { var personalForm = (Personel_Form)personel; // personalForm.Kimlik } 
  2. But a better approach is : instead of using the non generic IEnumerable use the generic IEnumerable<T> : 但是更好的方法是 :代替使用非通用IEnumerable使用通用IEnumerable<T>

     IEnumerable<Personel_Form> personeller = sicil_model.OlayForm_Personel(); 

As for your latest errors (from comments) it is because your function returns an anonymous type and not the Personel_Form type. 至于您最近的错误(来自注释),是因为您的函数返回的是匿名类型,而不是Personel_Form类型。 Instead if instantiating an anonymous object instantiate a Personel_Form : 相反,如果实例化一个匿名对象,则实例化一个Personel_Form

public IEnumerable<Personel_Form> OlayForm_Personel() 
{
    return (from p in Entity.GetEntity().Sicil
           select new Personel_Form
           {
            Id = p.Id,
            Kimlik = p.Isim + " " + p.Soyad,
            EMail = p.EMail,
            DepartmanAdi = p.Departman.DepartmanAdi,
            PozisyonAdi = p.Pozisyon1.PozisyonAdi
           }).ToList();
}

If its possible, you could change your method from 如果可能的话,您可以从

public object OlayForm_Personel() 
    {
        return (from p in Entity.GetEntity().Sicil
                           select new 
                           {
                            Id = p.Id,
                            Kimlik = p.Isim + " " + p.Soyad,
                            EMail = p.EMail,
                            DepartmanAdi = p.Departman.DepartmanAdi,
                            PozisyonAdi = p.Pozisyon1.PozisyonAdi
                           }).ToList();
    }

to

public IEnumerable<Personel_Form> OlayForm_Personel() 
    {
        return (from p in Entity.GetEntity().Sicil
                           select new Personel_Form
                           {
                            Id = p.Id,
                            Kimlik = p.Isim + " " + p.Soyad,
                            EMail = p.EMail,
                            DepartmanAdi = p.Departman.DepartmanAdi,
                            PozisyonAdi = p.Pozisyon1.PozisyonAdi
                           }).ToList();
    }

then you could change your consuming code to this 然后您可以将使用的代码更改为此

var personeller = sicil_model.OlayForm_Personel();

foreach (var personel in personeller)
{
    // personel.Kimlik
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM