投稿数 - 437, コメント - 5819, トラックバック - 156

LINQ Tips - 複数のキーを使って join

C# には join ステートメントがあり、2つのコレクションを結合できるのだが、結合のためのキーが一つしか指定できない。どうしたら複数のキーを使って結合できるのか?

キーに匿名型を使えば良いだけ。

using System;
using System.Linq;

class Data
{
    public int X { get; set; }
    public int Y { get; set; }
    public override string ToString()
    {
        return String.Format("X = {0}, Y = {1}", X, Y);
    }
}

class Program
{
    static void Main()
    {
        var data1 = new[] {
            new Data { X = 1, Y = 1 },
            new Data { X = 2, Y = 2 },
            new Data { X = 3, Y = 3 },
            new Data { X = 4, Y = 4 },
            new Data { X = 5, Y = 5 },
        };

        var data2 = new[] {
            new Data { X = 1, Y = 5 },
            new Data { X = 2, Y = 4 },
            new Data { X = 3, Y = 3 },
            new Data { X = 4, Y = 2 },
            new Data { X = 5, Y = 1 },
        };

        Console.WriteLine("単一の値で join");

        var query1 = from d1 in data1
                     join d2 in data2 on d1.X equals d2.X
                     select d1;

        query1.ToList().ForEach(Console.WriteLine);

        Console.WriteLine("複数の値で join");

        var query2 = from d1 in data1
                     join d2 in data2 on new { d1.X, d1.Y } equals new { d2.X, d2.Y }
                     select d1;

        query2.ToList().ForEach(Console.WriteLine);
    }
}

 join_run

投稿日時 : 2008年8月31日 12:31

フィードバック

# re: LINQ Tips - 複数のキーを使って join

ほー。
どうも equals での比較には IEqualityComparer が使われるようですな。
2008/08/31 17:44 | シャノン

# re: LINQ Tips - 複数のキーを使って join

なーんも考えんと普通に Equals 使っとるんかと思ってた。
2008/09/02 0:21 | 囚人

# http://lvshoulderbagbuy.sinaapp.com/

?婁笘?便伀?倞????傘?????亜?樹??с????
2012/10/20 2:59 | Louis Vuitton

# Wonderful blog! I found it while browsing on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Thanks

Wonderful blog! I found it while browsing on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?
I've been trying for a while but I never seem to get there!
Thanks

# Link exchange is nothing else however it is just placing the other person's blog link on your page at suitable place and other person will also do similar in favor of you.

Link exchange is nothing else however it is just
placing the other person's blog link on your page at suitable place and other person will also do similar
in favor of you.

# I constantly spent my half an hour to read this webpage's articles all the time along with a cup of coffee.

I constantly spent my half an hour to read this webpage's articles all the
time along with a cup of coffee.

# Can you tell us more about this? I'd like to find out more details.

Can you tell us more about this? I'd like to find out more details.

# Asking questions are really good thing if you are not understanding something fully, except this piece of writing offers fastidious understanding yet.

Asking questions are really good thing if you are not understanding
something fully, except this piece of writing offers fastidious understanding yet.

# Fascinating blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweeks would really make my blog stand out. Please let me know where you got your theme. Cheers

Fascinating blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple tweeks would
really make my blog stand out. Please let me know where you got your theme.
Cheers

# When someone writes an post he/she retains the idea of a user in his/her mind that how a user can be aware of it. Therefore that's why this piece of writing is outstdanding. Thanks!

When someone writes an post he/she retains the idea
of a user in his/her mind that how a user can be aware of it.
Therefore that's why this piece of writing is outstdanding.
Thanks!

# After looking into a few of the articles on your website, I really appreciate your way of writing a blog. I saved as a favorite it to my bookmark site list and will be checking back soon. Take a look at my web site as well and let me know what you think.

After looking into a few of the articles on your website,
I really appreciate your way of writing a blog.
I saved as a favorite it to my bookmark site list and will be checking back soon. Take a look
at my web site as well and let me know what you think.

# Sweet blog! I found it while searching on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Thanks

Sweet blog! I found it while searching on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?

I've been trying for a while but I never seem to get there!
Thanks

# I every time spent my half an hour to read this web site's posts everyday along with a mug of coffee.

I every time spent my half an hour to read this web site's posts everyday
along with a mug of coffee.

# Can I just say what a comfort to find someone who genuinely knows what they're discussing on the web. You actually know how to bring a problem to light and make it important. More and more people really need to check this out and understand this side o

Can I just say what a comfort to find someone who genuinely knows what they're discussing on the web.
You actually know how to bring a problem to light and
make it important. More and more people really need to check this out
and understand this side of the story. I
was surprised that you are not more popular because you
certainly possess the gift.

# Hey! I just wanted to ask if you ever have any trouble with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no backup. Do you have any solutions to prevent hackers?

Hey! I just wanted to ask if you ever have any
trouble with hackers? My last blog (wordpress) was hacked and
I ended up losing many months of hard work due to no
backup. Do you have any solutions to prevent hackers?

# Good day! I know this is kinda off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having difficulty finding one? Thanks a lot!

Good day! I know this is kinda off topic but I was wondering if you
knew where I could locate a captcha plugin for my comment form?

I'm using the same blog platform as yours and I'm having difficulty
finding one? Thanks a lot!

# Fastidious response in return of this difficulty with genuine arguments and telling everything about that.

Fastidious response in return of this difficulty with genuine arguments and telling everything about that.

# My programmer is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using WordPress on several websites for about a year and am nervous about switching to anothe

My programmer is trying to persuade me to move to .net from
PHP. I have always disliked the idea because of the costs.
But he's tryiong none the less. I've been using WordPress
on several websites for about a year and am nervous about switching to another platform.
I have heard excellent things about blogengine.net. Is there a way I can import all my wordpress posts into it?
Any help would be greatly appreciated!

コメントの投稿

タイトル  
名前  
URL
コメント