将 IEnumerable 转换为 EntitySet

发布于 2024-09-01 17:54:57 字数 2903 浏览 3 评论 0原文

希望有人能提供一些线索,或许还有一个可能的解决方案来解决我遇到的这个问题...

我已经使用 LINQ to SQL 将一些数据从数据库提取到本地实体中。它们是来自购物车系统的产品。产品可以包含 KitGroup 的集合(存储在 EntitySet (System.Data.Linq.EntitySet) 中。KitGroup 包含 KitItem 的集合,KitItem 可以包含嵌套产品(链接回原始 Product 类型 - 因此其 我

使用 LINQ to XML 构建 XML - 一切都很好 - 我的 XML 看起来很漂亮,调用了“GenerateProductElement”函数,该函数递归地调用自身来生成嵌套的产品。

从这些实体中, 我被卡住了..我现在正在尝试将该 XML 反序列化回原始对象(全部由 Linq to SQL 自动生成)...这就是问题所在。Linq to Sql 期望我的集合是 EntitySet 集合,但是 Linq to SQL 是这样的。 Xml(我试图用它来反序列化)正在返回 IEnumerable

我已经尝试了两种方法之间的转换,但似乎没有任何效果......我开始认为我应该手动反序列化。 (使用一些时髦的循环和条件来确定 KitGroup KitItems 属于哪个,等等)...但是它确实非常棘手,而且代码可能非常难看,所以我很想找到一个更优雅的解决方案来解决这个问题。

有什么建议吗?

这是一个代码片段:

    private Product GenerateProductFromXML(XDocument inDoc)
{
    var prod = from p in inDoc.Descendants("Product")
        select new Product
        {
            ProductID = (int)p.Attribute("ID"),
            ProductGUID = (Guid)p.Attribute("GUID"),
            Name = (string)p.Element("Name"),
            Summary = (string)p.Element("Summary"),
            Description = (string)p.Element("Description"),
            SEName = (string)p.Element("SEName"),
            SETitle = (string)p.Element("SETitle"),
            XmlPackage = (string)p.Element("XmlPackage"),
            IsAKit = (byte)(int)p.Element("IsAKit"),
            ExtensionData = (string)p.Element("ExtensionData"),
        };

    //TODO: UUGGGGGGG Converting b/w IEnumerable & EntitySet 
    var kitGroups = (from kg in inDoc.Descendants("KitGroups").Elements("KitGroup")
                     select new KitGroup
                                {
                                    KitGroupID = (int) kg.Attribute("ID"),
                                    KitGroupGUID = (Guid) kg.Attribute("GUID"),
                                    Name = (string) kg.Element("Name"),
                                    KitItems = // THIS IS WHERE IT FAILS - "Cannot convert source type IEnumerable to target type EntitySet..."
                                        (from ki in kg.Descendants("KitItems").Elements("KitItem")
                                         select new KitItem
                                                    {
                                                        KitItemID = (int) ki.Attribute("ID"),
                                                        KitItemGUID = (Guid) ki.Attribute("GUID")
                                                    });
                               });

    Product ImportedProduct = prod.First();

    ImportedProduct.KitGroups = new EntitySet<KitGroup>();
    ImportedProduct.KitGroups.AddRange(kitGroups);

    return ImportedProduct;
}
enter code here

我应该补充一点,这里提到的所有实体(Product、KitGroup、KitItem 等)都是由 Linq to SQL 生成的 - 没有映射回任何其他实体(购物车不使用实体,因此它们存在在这种情况下,仅作为一种从 xml 和数据库进行序列化/反序列化的方法。我正在构建的功能是能够从一个环境导出产品及其所有套件组、套件项目和嵌套产品,然后导入到另一个环境中。

Hoping somebody can shed some light, and perhaps a possible solution to this issue I'm having...

I have used LINQ to SQL to pull some data from a database into local entities. They are products from a shopping cart system. A product can contain a collection of KitGroups (which are stored in an EntitySet (System.Data.Linq.EntitySet). KitGroups contain collections of KitItems, and KitItems can contain Nested Products (which link back up to the original Product type - so its recursive).

From these entities I'm building XML using LINQ to XML - all good here - my XML looks beautiful, calling a "GenerateProductElement" function, which calls itself recursively to generate the nested products. Wonderful stuff.

However, here's where i'm stuck.. i'm now trying to deserialize that XML back to the original objects (all autogenerated by Linq to SQL)... and herein lies the problem. Linq tO Sql expects my collections to be EntitySet collections, however Linq to Xml (which i'm tyring to use to deserailise) is returning IEnumerable.

I've experimented with a few ways of casting between the 2, but nothing seems to work... I'm starting to think that I should just deserialise manually (with some funky loops and conditionals to determine which KitGroup KitItems belong to, etc)... however its really quite tricky and that code is likely to be quite ugly, so I'd love to find a more elegant solution to this problem.

Any suggestions?

Here's a code snippet:

    private Product GenerateProductFromXML(XDocument inDoc)
{
    var prod = from p in inDoc.Descendants("Product")
        select new Product
        {
            ProductID = (int)p.Attribute("ID"),
            ProductGUID = (Guid)p.Attribute("GUID"),
            Name = (string)p.Element("Name"),
            Summary = (string)p.Element("Summary"),
            Description = (string)p.Element("Description"),
            SEName = (string)p.Element("SEName"),
            SETitle = (string)p.Element("SETitle"),
            XmlPackage = (string)p.Element("XmlPackage"),
            IsAKit = (byte)(int)p.Element("IsAKit"),
            ExtensionData = (string)p.Element("ExtensionData"),
        };

    //TODO: UUGGGGGGG Converting b/w IEnumerable & EntitySet 
    var kitGroups = (from kg in inDoc.Descendants("KitGroups").Elements("KitGroup")
                     select new KitGroup
                                {
                                    KitGroupID = (int) kg.Attribute("ID"),
                                    KitGroupGUID = (Guid) kg.Attribute("GUID"),
                                    Name = (string) kg.Element("Name"),
                                    KitItems = // THIS IS WHERE IT FAILS - "Cannot convert source type IEnumerable to target type EntitySet..."
                                        (from ki in kg.Descendants("KitItems").Elements("KitItem")
                                         select new KitItem
                                                    {
                                                        KitItemID = (int) ki.Attribute("ID"),
                                                        KitItemGUID = (Guid) ki.Attribute("GUID")
                                                    });
                               });

    Product ImportedProduct = prod.First();

    ImportedProduct.KitGroups = new EntitySet<KitGroup>();
    ImportedProduct.KitGroups.AddRange(kitGroups);

    return ImportedProduct;
}
enter code here

I should add that all the entities mentioned here (Product, KitGroup, KitItem, etc) are generated by Linq to SQL - with no mapping back to any other entities (the shopping cart doesn't use entities so they exist in this context only as a means to serialise/deserialise to/from xml and the database. The functionality i'm building is the ability to export a product with all its kitgroups, kitItems and nested products from one environment, and import into another.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单爱 2024-09-08 17:54:57

发现以下链接有用。

http://social.msdn .microsoft.com/Forums/en-US/linqprojectgeneral/thread/58c4dcf8-2d89-4a3c-bb30-58c7c15df04b


编辑:如果上述链接中断,解决方案是创建一个扩展方法

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

Subquery 然后可以使用 <代码>.ToEntitySet()

...
(from ki in kg.Descendants("KitItems").Elements("KitItem")
select new KitItem
{
    KitItemID = (int) ki.Attribute("ID"),
    KitItemGUID = (Guid) ki.Attribute("GUID")
}).ToEntitySet();
...

Find following link useful.

http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/58c4dcf8-2d89-4a3c-bb30-58c7c15df04b


EDIT: in case the above link ever breaks, solution is to create an extension method

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

Subquery can then use .ToEntitySet()

...
(from ki in kg.Descendants("KitItems").Elements("KitItem")
select new KitItem
{
    KitItemID = (int) ki.Attribute("ID"),
    KitItemGUID = (Guid) ki.Attribute("GUID")
}).ToEntitySet();
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文