结合2个不同的类型(一对多)

时间:2019-07-19 10:07:31

标签: c# linq ienumerable

我必须这样列出:

                @foreach($user->contact as $contact)
                    <div id="contact<?php echo $count_user++; ?>" class="tab-pane magazine-sb-categories <?php if($count_user == 1){ echo "active"; } ?>">
                        <div class="row team-v1">


                        <ul class="list-unstyled col-xs-12" style="margin-bottom: -10px">
                    <li><h3 style="margin-top: 5px !important;text-transform: none; " >
                        @if($contact->role[0]->slug == "individuals")
                                        <i style="font-size: 13px;" class="icon-user"></i>

                                    @elseif($contact->role[0]->slug =='organizations')
                                        <i style="font-size: 13px;" class="icon-hotel-restaurant-172 u-line-icon-pro fa- fa-lg"></i>
                                    @endif

                                    <a style="font-size: 14px" href="{{ url('') }}/{{ $contact->username }}">{{ $contact->username  }}</a></h3>
                                <p>
                    <strong><i class="icon-real-estate-020 u-line-icon-pro"></i> : </strong><a>{{ $contact->country->country }}</a><br>
                    <strong><i class="icon-screen-tablet fa-" aria-hidden="true"></i> : </strong><a>{{ $contact->industry->industry }}</a><br>
                    @if($contact->role[0]->slug == "individuals")
                                                    @foreach($contact->career_path as $career_path)
                                                        <i style="font-size: 13px" class="icon-speedometer"></i> : {{ $career_path->functions->function }}
                                                        @break;
                                                    @endforeach
                                                @elseif($contact->role[0]->slug =='organizations')

                                                    <i style="font-size: 13px" class="icon-frame fa-"></i> : {{ $user->organization_type->organization_type }}<br>
                                                @endif


                        </p>
                    </ul>



                        </div>

                        @if(Sentinel::check())
                            @if(Sentinel::check()->username != $contact->username)

                                <ul class="social-icons social-icons-color" style="display:none;">
                                    <div class="social-cu">

                                        <li class="facebook_share" link="{{ url('') }}/{{ $contact->username }}">
                                            <a href="#" data-original-title="Facebook" class="rounded social_facebook"></a>
                                        </li>
                                        <li class="google_share" link="{{ url('') }}/{{ $contact->username }}" name="{{ $contact->username }} Workstickers Profile">
                                            <a href="#" data-original-title="Google Plus" class="rounded social_googleplus"></a>
                                        </li>
                                        <li class="twitter_share" link="{{ url('') }}/{{ $contact->username }}" name="{{ $contact->username }} Workstickers Profile">
                                            <a href="#" data-original-title="Twitter" class="rounded social_twitter"></a>
                                        </li>
                                        <li class="whatapp_share" link="{{ url('') }}/{{ $contact->username }}" name="{{ $contact->username }} Workstickers Profile">
                                            <a href="whatsapp://send?text={{ url('') }}/{{ $contact->username }}">
                                                <img class="img-responsive social_whatsapp" src="{{ asset('/assets/img/icons/social/Whatsapp.png') }}" alt="">
                                            </a>
                                        </li>
                                    </div>
                                </ul>

                                <ul class="list-inline share-list noticeboard-share-list" style="text-align: center;margin-bottom: -15px;top: -5px;position: relative">


                                    <li class="send-contact-message" data-user-id="{{ $contact->id }}">

                                        <a href="javascript:void(0)" data-toggle="modal" data-target="#exampleModal2"><i class="fa fa-paper-plane"></i>Message</a>
                                    </li>
                                    <li class="share-link">
                                        <a href="javascript:void(0)"><i class="fa fa-share-alt"></i>Share</a>
                                    </li>
                                </ul>
                                <br>


                            @endif
                        @endif

                    </div>

                    {{-- @if(Sentinel::check())
                        @if(Sentinel::check()->username != $contact->username)

                            <ul class="social-icons social-icons-color" style="display:none;">
                                <div class="social-cu">

                                    <li class="facebook_share" link="{{ url('') }}/{{ $contact->username }}">
                                        <a href="#" data-original-title="Facebook" class="rounded social_facebook"></a>
                                    </li>
                                    <li class="google_share" link="{{ url('') }}/{{ $contact->username }}" name="{{ $contact->username }} Workstickers Profile">
                                        <a href="#" data-original-title="Google Plus" class="rounded social_googleplus"></a>
                                    </li>
                                    <li class="twitter_share" link="{{ url('') }}/{{ $contact->username }}" name="{{ $contact->username }} Workstickers Profile">
                                        <a href="#" data-original-title="Twitter" class="rounded social_twitter"></a>
                                    </li>
                                    <li class="whatapp_share" link="{{ url('') }}/{{ $contact->username }}" name="{{ $contact->username }} Workstickers Profile">
                                        <a href="whatsapp://send?text={{ url('') }}/{{ $contact->username }}">
                                            <img class="img-responsive social_whatsapp" src="{{ asset('/assets/img/icons/social/Whatsapp.png') }}" alt="">
                                        </a>
                                    </li>
                                </div>
                            </ul>

                            <ul class="list-inline share-list noticeboard-share-list">

                                <li>
                                    <a href="{{ url('sendUserInvitation') }}/{{ $contact->id }}"><i style="font-size: 11px;" class="icon-size-fullscreen fa-s"></i>Invitation</a>
                                </li>
                                <li class="send-contact-message" data-user-id="{{ $contact->id }}">

                                        <a href="javascript:void(0)" data-toggle="modal" data-target="#exampleModal2"><i class="fa fa-paper-plane"></i>Message</a>
                                    </li>
                                <li class="share-link">
                                    <a href="javascript:void(0)"><i class="fa fa-share-alt"></i>Share</a>
                                </li>
                            </ul>
                            <br>


                        @endif
                    @endif --}}

                                    @endforeach

我想将列表合并为一个。将文章与所有度量单位组合在一个交流列表中,仅使用名称文章,ID度量单位,Name度量单位。

您可以为列表中的每个项目执行两个循环,但是我想知道是否有一种方法可以使linq变得更简单。

Foreach示例:

IEnumerable<Entities.Article> articlelist;
IEnumerable<Entities.MeasureUnit> measureList

1 个答案:

答案 0 :(得分:0)

您可以使用SelectMany来构建此笛卡尔积:

IEnumeable<Models.SearchItem> searchItems = articlelist
   .SelectMany(article => measureList, (a, m) 
      => new Models.SearchItem() { IDItem = m.IDMeasureUnit, PrincipalProperty = a.Description, SecondaryProperty = m.Description });