Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将IEnumerable转换为EntitySet_C#_.net_Linq To Sql_Linq To Xml_Ienumerable - Fatal编程技术网

C# 将IEnumerable转换为EntitySet

C# 将IEnumerable转换为EntitySet,c#,.net,linq-to-sql,linq-to-xml,ienumerable,C#,.net,Linq To Sql,Linq To Xml,Ienumerable,希望有人能给我一些启示,也许是解决这个问题的可能方法 我使用LINQtoSQL将一些数据从数据库拉入本地实体。它们是购物车系统的产品。产品可以包含KitGroups集合(存储在EntitySet(System.Data.Linq.EntitySet)中)。KitGroups包含KitItems集合,KitItems可以包含嵌套产品(链接回原始产品类型,因此是递归的) 从这些实体中,我使用LINQ到XML构建XML——这里一切都很好——我的XML看起来很漂亮,调用了“GenerateProduct

希望有人能给我一些启示,也许是解决这个问题的可能方法

我使用LINQtoSQL将一些数据从数据库拉入本地实体。它们是购物车系统的产品。产品可以包含KitGroups集合(存储在EntitySet(System.Data.Linq.EntitySet)中)。KitGroups包含KitItems集合,KitItems可以包含嵌套产品(链接回原始产品类型,因此是递归的)

从这些实体中,我使用LINQ到XML构建XML——这里一切都很好——我的XML看起来很漂亮,调用了“GenerateProductElement”函数,它递归地调用自己来生成嵌套的产品。很棒的东西

然而,我的问题就在这里。我现在正试图将XML反序列化回原始对象(所有由Linq to SQL自动生成)……问题就在这里。Linq to SQL希望我的集合是EntitySet集合,而Linq to XML(我正打算使用它反序列化)返回IEnumerable

我已经尝试了一些在2之间进行强制转换的方法,但似乎没有任何效果……我开始认为我应该手动反序列化(使用一些时髦的循环和条件来确定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
私有产品GenerateProductFromXML(XDocument inDoc)
{
var prod=来自印度支系后代(“产品”)中的p
选择新产品
{
ProductID=(int)p.Attribute(“ID”),
ProductGUID=(Guid)p.Attribute(“Guid”),
名称=(字符串)p.Element(“名称”),
Summary=(字符串)p.Element(“Summary”),
Description=(字符串)p.Element(“Description”),
SEName=(字符串)p.Element(“SEName”),
SETitle=(字符串)p.Element(“SETitle”),
XmlPackage=(字符串)p.Element(“XmlPackage”),
IsAKit=(字节)(int)p.Element(“IsAKit”),
ExtensionData=(字符串)p.Element(“ExtensionData”),
};
//TODO:uugggg转换b/w IEnumerable和EntitySet
var kitGroups=(源自印支语后代(“kitGroups”).元素(“KitGroup”)中的kg)
选择新组
{
KitGroupID=(int)kg.Attribute(“ID”),
KitGroupGUID=(Guid)kg.Attribute(“Guid”),
名称=(字符串)kg.Element(“名称”),
KitItems=//这就是它失败的地方-“无法将源类型IEnumerable转换为目标类型EntitySet…”
(来自ki,单位为kg.子代(“KitItems”).元素(“KitItem”)
选择新项目
{
KitItemID=(int)ki.Attribute(“ID”),
KitItemGUID=(Guid)ki.Attribute(“Guid”)
});
});
产品进口产品=产品第一();
ImportedProduct.KitGroups=new EntitySet();
ImportedProduct.KitGroups.AddRange(KitGroups);
退货进口产品;
}
在这里输入代码

我应该补充一点,这里提到的所有实体(Product、KitGroup、KitItem等)都是由Linq到SQL生成的,没有映射回任何其他实体(购物车不使用实体,因此它们在此上下文中仅作为序列化/反序列化xml和数据库的手段。我正在构建的功能是能够从一个环境导出产品及其所有KitGroup、kitItems和嵌套产品,并导入到另一个环境中。

发现以下链接有用


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

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

啊,是的,那正是我想要的…谢谢你,Jinal!谢谢你,伙计,你救了我。很久以来我一直在为同样的问题挣扎。。。
...
(from ki in kg.Descendants("KitItems").Elements("KitItem")
select new KitItem
{
    KitItemID = (int) ki.Attribute("ID"),
    KitItemGUID = (Guid) ki.Attribute("GUID")
}).ToEntitySet();
...