我要如何填充从LINQ2SQL查询POCO(子)IList的财产? [英] How do i populate a POCO (child) IList property from a Linq2Sql query?

查看:99
本文介绍了我要如何填充从LINQ2SQL查询POCO(子)IList的财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类:

public class Question
{
    public IList<Answer> Answers { get; set; }
}

public class Answer
{ .. }

在我的LINQ2SQL设计师,还有对设计师2 L2S对象,用正确的0℃ - >它们之间的许多箭头。 KEWL。

In my Linq2Sql designer, there's two L2S objects on designer, with the correct 0<->many arrow between them. Kewl.

我不知道我怎么能找回这些问题/答案在一个单一的通话和填充我POCO对象。

I'm not sure how i can retrieve these questions/answers in a single call and populate my POCO objects ..

这是我已经得到了...有人可以填补这一空白?

this is what i've got ... can someone fill in the blanks?

public IQueryable<Question> GetQuestions()
{
    return from q in _db.Questions
        select new Question
            {
                Title = q.Title,
                Answers = ????????   // <-- HALP! :)
            };
}

想法?

感谢您的答复,但它不是100%的还没有。

Thanks for the replies but it's not 100% there yet.

首先,我要回一个POCO类,而不是LINQ2SQL上下文类。这就是为什么我在做什么...

Firstly, i'm returning a POCO class, not the Linq2Sql context class. This is why i'm doing...

select new Question { .. };

这类是POCO,不是LINQ2SQL。

that class is POCO, not the linq2sql.

其次,我喜欢这点做答案的答案= q.Answers.ToList(),但是这也不会工作,因为该公司试图以一个LINQ2SQL类设置为POCO类。

Secondly, I like the answers that point to doing Answers = q.Answers.ToList() but this also will not work because it's trying to set a Linq2Sql class to a POCO class.

推荐答案

如果您的LINQ to SQL类有它们之间的关系,那么答案属性应已生成的问题的LINQ to SQL类。所以,你应该能够简单地做到这一点:

If your LINQ to SQL classes have a relationship between them, then an "Answers" property should have been generated on the "Question" LINQ to SQL class. So you should be able to simply do this:

return from q in _db.Questions
       select new Question
       {
           Title = q.Title,
           Answers = q.Answers.ToList(),
       }

您可能能够省略调用了ToList() - 我不知道使用什么类型的LINQ to SQL的生成相关行(我相信这是的IQueryable&LT; T&GT; )。

You may be able to omit the call to ToList() - I'm not sure what type LINQ to SQL uses for generated related rows (I believe it's IQueryable<T>).

要强制回答属性被填充了前面,而不是发射了另外一个询问每一个问题,你可以使用的 DataLoadOptions 类。基本上你告诉LINQ到SQL负载的回答你的任何问题,查询时间(使用LoadWith方法 - 请参阅MSDN文档)

To force the Answers property to be populated up front, rather than firing off another query for each question, you can use the DataLoadOptions class. Essentially you tell LINQ to SQL to load answers any time you query questions (using the LoadWith method - see the MSDN documentation).

修改

您是对的 - 因为你的Question.Answers财产是你自己的POCO的名单,与q.Answers分配是不是要走的路。这可能会更好地工作:

You're right - since your Question.Answers property is a list of your own POCO, assigning it with q.Answers isn't the way to go. This might work better:

return from q in _db.Questions
       select new Question
       {
           Title = q.Title,
           Answers = (from a in q.Answers
                     select new Answer { ... }).ToList(),
       }

这篇关于我要如何填充从LINQ2SQL查询POCO(子)IList的财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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