[英]Convert NHibernate IList to List in C#
I have NHibernate IList.我有 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:这是我将其传递给 jQuery UI 自动完成的代码:
[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]].无法序列化接口 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.将我的 IList 传递给 JSON 的任何解决方案都会对我有所帮助。 Thank you.
谢谢你。
Using LINQ:使用 LINQ:
return tagList.ToList();
Use.ToList() extension method:使用.ToList() 扩展方法:
return list.ToList()
You need to change the return type to Tag[] and use ToArray() instead of ToList().您需要将返回类型更改为 Tag[] 并使用 ToArray() 而不是 ToList()。 IIRC, you cannot serialize an IList in a webmethod.
IIRC,您不能在 webmethod 中序列化 IList。
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.根据您对答案的评论,我会改用AutoMapper 。
return Tag.GetAll().ToList() does not work for you? return Tag.GetAll().ToList() 对你不起作用?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.