SQL / Linq查询选择多对多

时间:2017-06-21 13:49:55

标签: c# sql entity-framework linq

我的SQL数据库中有以下(简化)结构。 enter image description here

现在我想选择用户尚未加入的所有群组。 如果使用Linq / Entity Framework编写查询会有所帮助,但我也可以使用SQL语句。

我尝试使用Include(使用EF):

.Include(g => g.Group_User) 

在查询中而不是使用:

.Where(g => g.Group_User.UserId != userId) 

但这不起作用,可能完全不是执行这类查询的正确方法。

感谢您阅读

1 个答案:

答案 0 :(得分:2)

  

我想选择用户尚未参与的所有群组。

因此,您希望没有群组用户的所有群组都使用此!Any,因此请使用==var q = db.Group.Include(g => g.Group_User) .Where(g => !g.Group_User.Any(gu => gu.UserId == userId));

<div class="row pt-5" id="acts">
        <div class="col-md-4 offset-md-1">
            <div class="card">
                <!-- Nav tabs -->
                <ul class="nav nav-tabs" role="tablist">
                    <li class="nav-item">
                        <a class="nav-link active" data-toggle="tab" href="#act1" role="tab">Take Five</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" data-toggle="tab" href="#act2" role="tab">Thank You For Your Consideration</a>
                    </li>
                </ul>

<!-- Tab panes -->
                <div class="tab-content">
                    <div class="tab-pane active" id="act1" role="tabpanel">
                        <div class="card-block">
                            <h3 class="card-title">Take Five</h3>
                            <p class="card-text">Do you also have trouble getting out of bed? Working 9 to 5, the same routine every day.. “Take Five” is about one of those days,  a light hearted piece full of absurdness and acrobatics. People of all ages can enjoy the 7- minute act,  let Ida surprise you!</p>

                        </div>
                    </div>
                    <div class="tab-pane" id="act2" role="tabpanel">
                        <div class="card-block">
                            <div class="container">
                                <div class="row">
                                    <div class="col-md-9">
                                        <h3 class="card-title">Thank You For Your Consideration</h3>
                                        <p class="card-text">What do you write in a letter when you apply for a job? Everybody has been there, behind a computer screen, mind blank. It sounds too pretentious or too insecure. If only they would see the real you. But if that’s a good idea.. An act about struggles that everyone has experienced, brought with humour, brutal honesty and creativity in movement. Ida’s genuine story is a 6 minute text-based circus performance.</p>
                                    </div>
                                    <div class="col-md-3">
                                        <img src="" alt="TYFYC" class="img-fluid img-rounded">
                                    </div>
                                </div>j
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-5 offset-md-2">
            <div class="card">
                <div class="tab-content">
                    <div class="tab-pane active" id="act1" role="tabpanel">
                        <div class="card-block">
                            <div class="embed-responsive embed-responsive-16by9">
                                <iframe class="embed-responsive-item" src="https://player.vimeo.com/video/220114759"  allowfullscreen></iframe>
                            </div>
                        </div>
                    </div>
                    <div class="tab-pane" id="act2" role="tabpanel">
                        Stuff number 2
                    </div>
                </div>
            </div>
        </div>
    </div>