Mostafa Azarirad
Mostafa Azarirad

Reputation: 647

how return an IEnumerable<> of my customized model?

I am using EF6 and one of my models name is tblAttachLabel. and I have customized model that name is AttachLabel. I need an IEnumerable of my customized model that fill with tblAttachLabel Model. It is very easy to return

IEnumerable<tblAttachLabel> 

from my function but I need to return

IEnumerable<AttachLabel>

so I do this code:

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;
    }

but I know when I use a List for temp the query will execute, but I dont want this. so how I can return an IEnumerable ?

Upvotes: 0

Views: 109

Answers (4)

David Frankland
David Frankland

Reputation: 21

One of these, depending if you prefer lambda expressions:

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

or not:

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
                };
    }
}

Upvotes: 0

t3chb0t
t3chb0t

Reputation: 18675

The other possibilty and an alternative to @haim770's answer is a loop with the yield keyword:

When you use the yield keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator. Using yield to define an iterator removes the need for an explicit extra class (the class that holds the state for an enumeration, see IEnumerator for an example) when you implement the IEnumerable and IEnumerator pattern for a custom collection type.

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;
}

Your context should also be disposed so I've added a using statement.

and there is no need for:

from al in db.tblAttachLabels select al;

because it just returns the same collection as db.tblAttachLabels.

Upvotes: 1

Shami Qureshi
Shami Qureshi

Reputation: 69

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
    });

}

Upvotes: 1

haim770
haim770

Reputation: 49105

Try this instead:

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

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

Upvotes: 2

Related Questions