How do you cast an IEnumerable or IQueryable to an EntitySet?

前端 未结 1 1766
無奈伤痛
無奈伤痛 2020-12-31 08:52

In this situation I am trying to perform a data import from an XML file to a database using LINQ to XML and LINQ to SQL.

Here\'s my LINQ data model:

         


        
相关标签:
1条回答
  • 2020-12-31 09:34

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

    and use it like so :

    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()
    
    0 讨论(0)
提交回复
热议问题