开发者

checking null condition with linq to entites using multiple joins

开发者 https://www.devze.com 2023-04-05 15:08 出处:网络
I have query like this ...below... I want to check the null condition ... if lastname(entered name) is empty dont check this condition ...

I have query like this ...below... I want to check the null condition ...

if lastname(entered name) is empty dont check this condition ...

report.member_Lastname.StartsWith(lastname)

if cardnum is empty dont check this condition

&& report.member_CardNum == cardnum

I am using linq to entities and i am not using linq to sql

var memberreport = from report in eclipse.members
                   join memtomship in eclipse.membertomships on report.member_Id equals memtomship.member_Id
                   join mshoption in eclipse.mshipoptions on memtomship.mshipOption_Id equals mshoption.mshipOption_Id
                   join membershiptypes in eclipse.mshiptypes on mshoption.mshipType_Id equals membershiptypes.mshipType_Id
                   join membershipstatustypes in eclipse.mshipstatustypes on memtomship.mshipStatusType_Id equals membershipstatustypes.mshipStatusType_Id                             
                   where report.member_Lastname.StartsWith(lastname)
                      && report.member_CardNum == cardnum 
                      && report.member_Postcode.StartsWith(postcode)
                      && report.member_Reference.StartsWith(refernce)
                      && report.member_Gender.StartsWith(gender)
                      && membershiptypes.mshipType_Name.StartsWith(membershiptype)
                      && membershipstatustypes.mshipStatusType_Name.StartsWith(membershipstatustypesa)
                      && string.Compare(report.member_Dob,maxage) >= 0
                      && string.Compare(report.member_Dob, minage)< 0
                   select new
                   {
                       report.member_Id,
                       report.member_Lastname,
                       report.member_Firstname,
                       report.member_Postcode,
                       re开发者_如何学Pythonport.member_Reference,
                       report.member_CardNum,
                       report.member_IsBiometric,
                       report.member_Dob,
                       membershiptypes.mshipType_Name,
                       membershipstatustypes.mshipStatusType_Name,
                       memtomship.memberToMship_EndDate
                   };   

would you guys pls suggest any idea and any code sample snippet for this..

Many thanks in advance ....

Modify :

if(!string.IsNullOrEmpty(lastname))
    query = query.Where(r => r.member_Lastname == lastname);

i want something like above, i know this query using linq to sql but i am using linq to entities


&& works stops on first false so you can write

...                          
where  
   (report.member_Lastname != null && report.member_Lastname.StartsWith(lastname))
&& ...

same with || and first true, so if you want Lastname to be null or startwith lastname you can use

...                          
where  
   (report.member_Lastname == null || report.member_Lastname.StartsWith(lastname))
&& ...

same with cardnum and wherever you want it. To get the values you can use

report.member_Lastname ?? "yourNullDefault"

assuming member_Lastname is a string

0

精彩评论

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