【问题标题】:How do you cast an IEnumerable<t> or IQueryable<t> to an EntitySet<t>?如何将 IEnumerable<t> 或 IQueryable<t> 转换为 EntitySet<t>?
【发布时间】:2010-09-11 04:19:08
【问题描述】:

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

这是我的 LINQ 数据模型:

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 数据上下文。

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() 构造函数似乎有帮助。

那么,我可以编写一个 LINQ 查询,用我的 IEnumerable 数据填充 EntitySet 数据吗?

【问题讨论】:

    标签: c# .net linq


    【解决方案1】:

    您可以使用帮助类从 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;
        }
    }
    

    并像这样使用它:

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

    【讨论】:

    • 希望我可以为你投票十几次 - 正好在这个问题上旋转我的轮子。你不仅帮助我前进,而且让我看到了扩展方法的整个概念——非常酷。
    • 您能否解释一下我必须如何实现公共静态类 EntityCollectionHelper 才能使其在 (DOT) }).ToEntitySet() 下工作?谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2010-11-18
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多