转换IEnumerable< T>到EntitySet< T> [英] Convert IEnumerable<T> to EntitySet<T>

查看:94
本文介绍了转换IEnumerable< T>到EntitySet< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如何转换IEnumerable< T>到EntitySet< T>?

var questions =来自e.Element中的q("问题")。元素("问题")

选择新问题
{
Text = q.Attribute (" title")。Value,
Point = Convert.ToDecimal(q.Attribute(" point"。)。),
Choices = from c在q.Elements("Choices")中。元素("Choice")
选择新的选择
{
Text = c.Attribute(" title")。Value,
Correct = Convert.ToBoolean(c.Attribute(" isCorrect")。Value)
}

;

以粗体显示的行抛出错误:

错误1无法隐式将类型'System.Collections.Generic.IEnumerable< DomainObjects.Choice>'转换为'System.Data.Linq.EntityS等人LT; DomainObjects.Choice>"。存在显式转换(您是否缺少演员表?)C:\Projects \ExamDLINQ \ExamDLINQSolution\ExamDLINQConsole\Program.cs 29 43 ExamDLINQConsole


< br>

解决方案

我认为没有任何内置方法可以实现这一目标。推测问题是一个LINQ to SQL实体,你想在之后插入到表中?

一种解决方案是编写自己的ToEntitySet扩展方法:

public static EntitySet< T> ToEntitySet< T> (此IEnumerable< T>源)其中T:class
{
var es = new EntitySet< T> ();
es.AddRange(source);
return es;
}

然后你可以编写你的查询如下:



var questions = from e inElement(" Questions" ;)元素("问题") 结果


Hi,

How can I convert IEnumerable<T> to EntitySet<T>?

 var questions = from q in e.Element("Questions").Elements("Question")

                            select new Question
                            {
                                Text = q.Attribute("title").Value,
                                Point = Convert.ToDecimal(q.Attribute("point").Value),
                                Choices = from c in q.Elements("Choices").Elements("Choice")
                                          select new Choice
                                          {                                         
                                              Text = c.Attribute("title").Value,
                                              Correct = Convert.ToBoolean(c.Attribute("isCorrect").Value)
                                          }                                          
                            };

The lines in bold throw the error:

Error    1    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<DomainObjects.Choice>' to 'System.Data.Linq.EntitySet<DomainObjects.Choice>'. An explicit conversion exists (are you missing a cast?)    C:\Projects\ExamDLINQ\ExamDLINQSolution\ExamDLINQConsole\Program.cs    29    43    ExamDLINQConsole



                           

解决方案

I don't think there's any built-in way to achieve this. Presumably Question is a LINQ to SQL entity which you want to insert into the table afterward?

One solution would be to write your own ToEntitySet 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;
    }

Then you could write your query as follows:

var questions = from q in e.Element("Questions").Elements("Question")


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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆