你能让这个多对多脚本更加动态吗?

发布于 12-09 10:33 字数 884 浏览 1 评论 0原文

这只是我建立多对多关系的简单方法,但我想要一些可以轻松创建多对多关系的方法,我希望我可以创建一个方法返回类型匿名,这将打开一些大门。但我不擅长匿名类型,但是!

希望您能帮忙!

当你需要的时候,你们会做什么

private class Ids
{
    public int Id;
}

...

。以某种方式:

IList<Ids> objIds = new List<Ids>();

var q = from c in dt.FK_Tags_Blogs
        where c.BlogId == someId
        select c.TagId;

foreach (var item in q)
{
    objIds.Add(new Ids { Id = item });
}

var w = from c in objIds
        join p in dt.Tags on c.Id equals p.Id
        select p;

David B:我这样使用你的代码:

    public static IQueryable<Tag> printTags(int id)
{
    DataClassesDataContext dt = new DataClassesDataContext();

     return 
      from b in dt.Blogs
      where b.Id == id
      from xr in b.FK_Tags_Blogs
      select xr.Tag;
}

这很愚蠢吗?

This is just my simple way of making a many to many relationship, but i would like something that makes it easy to create many to many, I have wished i could make a method return type anonymous, that would open some doors. But i am noot good with anonymous types, Yet!

Hope you can help !

What do you guys do when you need

private class Ids
{
    public int Id;
}

.

...

. And in some method:

IList<Ids> objIds = new List<Ids>();

var q = from c in dt.FK_Tags_Blogs
        where c.BlogId == someId
        select c.TagId;

foreach (var item in q)
{
    objIds.Add(new Ids { Id = item });
}

var w = from c in objIds
        join p in dt.Tags on c.Id equals p.Id
        select p;

David B: I Used your code like this:

    public static IQueryable<Tag> printTags(int id)
{
    DataClassesDataContext dt = new DataClassesDataContext();

     return 
      from b in dt.Blogs
      where b.Id == id
      from xr in b.FK_Tags_Blogs
      select xr.Tag;
}

Is that stupid?

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

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

发布评论

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

评论(1

醉殇2024-12-16 10:33:32

我所做的是:

  //linq to objects
IEnumerable<Tag> tags = 
  from c in dt.FK_Tags_Blogs
  where c.BlogId == someId
  join p in dt.Tags on c.TagId equals p.Id
  select p;


  //linq to sql with association properties
IQueryable<Tag> tagsQuery =
  from b in dc.Blogs
  where b.BlogId == someId
  from xr in b.FK_Tags_Blogs
  select xr.Tag;

What I do is:

  //linq to objects
IEnumerable<Tag> tags = 
  from c in dt.FK_Tags_Blogs
  where c.BlogId == someId
  join p in dt.Tags on c.TagId equals p.Id
  select p;


  //linq to sql with association properties
IQueryable<Tag> tagsQuery =
  from b in dc.Blogs
  where b.BlogId == someId
  from xr in b.FK_Tags_Blogs
  select xr.Tag;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文