Mr. D
Mr. D

Reputation: 23

Convert NHibernate IList to List in C#

I have NHibernate IList. I want to convert to a List. Please help me.

Here is my code

IList<Tag> list = Tag.GetAll();

I want to convert this to list

public class CategoryTag
{
    public int ID { get; set; }
    public string TagName { get; set; }

    public CategoryTag()
    {

    }

    public List<CategoryTag> GetCategoryTagList()
    {
         IList<Tag> list = Tag.GetAll();

         // How do I return Tag as List?

        return tagList;
    }
}

Update

I want to update my question since my question is not well explained.

Here is my code to pass it to jQuery UI Autocomplete:

[WebMethod]
    public IList<Tag> FetchTagList(string tag)
    {
        var ctag = new List<Tag>(Tag.GetAll())
            .Where(m => m.TagName.ToLower().StartsWith(tag.ToLower()));
        return ctag.ToList();
    }

I get following error:

Cannot serialize interface System.Collections.Generic.IList`1[[QuestionCenter.Domain.Tag, QuestionCenter.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].

Any solution to pass my IList to JSON will help me. Thank you.

Upvotes: 0

Views: 2196

Answers (5)

Juanma
Juanma

Reputation: 3981

You need to change the return type to Tag[] and use ToArray() instead of ToList(). IIRC, you cannot serialize an IList in a webmethod.

Upvotes: 1

jgauffin
jgauffin

Reputation: 101150

var myList = Tag.GetAll()
                 .ConvertAll(tag => new CategoryTag{ Id = tag.Id, XXX = tag.YYY})
                 .ToList();

Update

Based on your comment to your answer I would use AutoMapper instead.

Upvotes: 0

Meta-Knight
Meta-Knight

Reputation: 17845

Use .ToList() extension method:

return list.ToList()

Upvotes: 2

epitka
epitka

Reputation: 17637

return Tag.GetAll().ToList() does not work for you?

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190945

Using LINQ:

return tagList.ToList();

Upvotes: 2

Related Questions