本文介绍了您如何转换IEnumerable< t>或IQueryable< t>到EntitySet< t&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我试图使用LINQ to XML和LINQ to SQL将数据从XML文件导入数据库.

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数据模型:

Here's my LINQ data 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;
}

基本上,我想做的是编写一个查询,该查询将为我提供一个可以提交给LINQ数据上下文的数据结构.

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.

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

问题出在子查询中.我必须以某种方式将我的对象集合放入EntitySet容器中.我无法转换它(哦,天哪,我是怎么尝试的),而且似乎没有EntitySet()构造函数会有所帮助.

The problem is in the sub-query. I have to somehow get my object collection into the EntitySet container. I can't cast it (oh lord how I've tried) and there's no EntitySet() constructor that would seem to help.

因此,我可以编写一个LINQ查询,该查询将填充EntitySet< PageContent>吗?我的IEnumerable< Page>中的数据数据?

So, can I write a LINQ query that will populate the EntitySet<PageContent> data with my IEnumerable<Page> data?

推荐答案

您可以使用助手类从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()

这篇关于您如何转换IEnumerable&lt; t&gt;或IQueryable&lt; t&gt;到EntitySet&lt; t&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 02:58