且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

您如何转换IEnumerable< t>或IQueryable< t>到EntitySet< t> ;?

更新时间:2021-07-15 15:45:16

您可以使用助手类从IEnumerable构建实体集,例如:

you can construct your entity set from a IEnumerable using a helper class, something like:

public static class EntityCollectionHelper
{
    public static EntitySet<T> ToEntitySet<T>(this IEnumerable<T> source) where T:class
    {
        EntitySet<T> set = new EntitySet<T>();
        set.AddRange(source);
        return set;
    }
}

并像这样使用它:

PageContents = (from pc in el.Elements()
                                where pc.Name.LocalName == "revision"
                                select new PageContent()
                                {
                                   Content = pc.Elements().Where(e => e.Name.LocalName=="text").First().Value,
                                   Username = pc.Elements().Where(e => e.Name.LocalName == "contributor").First().Elements().Where(e => e.Name.LocalName == "username").First().Value,
                                   DateTime = DateTime.Parse(pc.Elements().Where(e => e.Name.LocalName == "timestamp").First().Value)
                                }).ToEntitySet()