开发者

IN Operator in where clause

开发者 https://www.devze.com 2022-12-18 16:04 出处:网络
List<HelprClass.Organizer> org = ( from EventOrg in cntx.EventOrganizer from MstrOrg in cntx.Organizer
List<HelprClass.Organizer> org = 
    ( from EventOrg in cntx.EventOrganizer 
    from MstrOrg in cntx.Organizer
    where EventOrg.OrganizerID == MstrOrg.OrganizerID
    Select new HelprClass.Organizer
    {
    OrganizerName = MstrOrg.OrganizerName
    }).ToList()

This work fine now i want to use IN Opeartor in the above Query.

in the EventOrganizer I have EventID now i w开发者_JAVA百科ant to select only Event ID exsist in EventOrganizer collection

I have EventID another var varibale

Var EventID= From EvntID in Evetn Select new {ID= EvntID.EventID};

Something like this

where

    EventOrg.OrganizerID == MstrOrg.OrganizerID

    &&  EventOrg.EventID in EventID.ID

How I can achive this ?

I will appreciate your help


Try this:

var EventIDs = from EvntID in Event select EvntID.EventID;

var org = (from EventOrg in cntx.EventOrganizer 
           from MstrOrg in cntx.Organizer
           where EventOrg.OrganizerID == MstrOrg.OrganizerID
           select new {E=EventOrg, M=MstrOrg}
          ).ToList();


org = org
        .Where(o => EventIDs.Contains(o.E.EventID) )
        .Select(o => new HelprClass.Organizer
           {
                OrganizerName = o.M.OrganizerName
           }
         );  


If you are using Entity Framework you can not use a Contains statement, which would be an easy solution in Linq. SO if it's just Linq 2 Entities, use a where clause like "where EventId.Contains(EventOrg.Id)"

If you are indeed using Entity Framework you will have to build an or expression based on the Id's in the EventID Collection.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号