LINQ2SQL许多:很多问题,你会怎么做呢? [英] Linq2Sql Many:Many question, How would you do this?

查看:154
本文介绍了LINQ2SQL许多:很多问题,你会怎么做呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道很多:许多不LINQ2SQL支持,但我工作的一种变通方法。

I know many:many isn't supported in Linq2Sql but I am working on a workaround

我与我的工作一点,所以克隆和我有一个表问题和标签和链接表QuestionTag表,所以我有很多经典:问题与标签之间一对多的关系。

I am working with my little SO clone and I have a table with Questions and a table with Tags and a linking table QuestionTag so I have a classic many:many relationship between Questions and Tags.

要显示的头版我有这个类我想从LINQ2SQL查询

To display the list of Questions on the front page I have this class I want to fill up from a Linq2Sql query

public class ListQuestion
{
   public int QuestionID { get; set; }
   public string Title{ get; set; }
   public IEnumerable<Tag> Tags { get; set; }
}


public IEnumerable<ListQuestion> GetQuestions()
{
   from q in Questions
   .................
   select new ListQuestion{ ... }
}

问题是我应该怎么填写标签集合。
我发现这是不可能在1单查询这样做,我也分为2个查询,1得到的问题和1本,以获得标签和稍后尝试加入他们的行列。
我知道很多:很多在实体框架,所以他们是如何做到这一点的支持?你会怎么做呢?任何替代方法?当然的查询应该是有效的。

The problem is how should I fill up the Tag collection. I found out this isn't possible to do in 1 single query so I have divided this into 2 queries, 1 to get the questions and 1 to get the tags and later try to join them. I know many:many is supported in Entity framework so how do they do it? How would you do this? Any alternative approach? The query should of course be efficient.

推荐答案

这可能适用于你的情况;

This may work for your case;

from q in Questions
select new ListQuestion 
{ 
  Tags = q.QuestionTags.Select(qt => qt.Tag),
  QuestionId = q.ID,
  Title = q.Title
}

这篇关于LINQ2SQL许多:很多问题,你会怎么做呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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