Reputation: 23
I have NHibernate IList. I want to convert to a List. Please help me.
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;
}
}
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
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
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