简体   繁体   English

你如何投射 IEnumerable<t> 或可查询<t>实体集<t> ?</t></t></t>

[英]How do you cast an IEnumerable<t> or IQueryable<t> to an EntitySet<t>?

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.在这种情况下,我尝试使用 LINQ 到 XML 和 LINQ 到 SQL 执行从 XML 文件到数据库的数据导入。

Here's my LINQ data model:这是我的 LINQ 数据 model:

public struct Page
{
    public string Name;
    public char Status;
    public EntitySet<PageContent> PageContents;

}
public struct PageContent
{
    public string Content;
    public string Username;
    public DateTime DateTime;
}

Basically what I'm trying to do is write a query that will give me a data structure that I can just submit to my LINQ Data Context.基本上我想做的是编写一个查询,该查询将为我提供一个数据结构,我可以将其提交到我的 LINQ 数据上下文。

IEnumerable<Page> pages = from el in doc.Descendants()
                          where el.Name.LocalName == "page"
                          select new Page()
                {
                    Name = el.Elements().Where(e => e.Name.LocalName == "title").First().Value,
                    Status = 'N',
                    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)
                                    }).ToList()
                };

The problem is in the sub-query.问题出在子查询中。 I have to somehow get my object collection into the EntitySet container.我必须以某种方式将我的 object 集合放入 EntitySet 容器中。 I can't cast it (oh lord how I've tried) and there's no EntitySet() constructor that would seem to help.我无法施放它(天哪,我怎么试过)而且没有 EntitySet() 构造函数似乎有帮助。

So, can I write a LINQ query that will populate the EntitySet<PageContent> data with my IEnumerable<Page> data?那么,我能否编写一个 LINQ 查询,用我的 IEnumerable<Page> 数据填充 EntitySet<PageContent> 数据?

you can construct your entity set from a IEnumerable using a helper class, something like:您可以使用帮助器 class 从 IEnumerable 构建您的实体集,例如:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM