开发者

two where clause in single linq query

I have a table memberaccounts

              member_id
              member_name
              membertransactionamount
              memberaccount_id

and my entity name is databasecontextsearch

and i am binding these details to datagrid view by using foolwing way

        var memberdetails = from members in databasecontextsearch.memberacconuts
                              select new 
                               {  
                                  memberid = members.member_id
                                  name = members.member_name
                                  amount = members.membertransactionamount
                                 };
                         bindingsource1.datasource = memberdetails;
                        datagridview1.datasource = bindingsource1;

and i have a text box (txtsrch) and a button (search)

when ever the user enter the member_name in text box(txtsrch) the the member det开发者_StackOverflowails will be populated with datagrid view according to that member_name..

when ever the user enter the membertransactionamount in the textbox(txtsrch) datagrid view will be populated with member details those who have that membertransactionamount..

is it possible these conditions in single if condition using linq to entities...


You can "transform" this into an AND:

var memberdetails = from members in databasecontextsearch.memberacconuts
                    where 
                    string.IsNullOrEmpty(name_param)?true:members.memebername==name_param
                    &&
                    ((tran_param!=null?members.membertransactionamount==tran_param:true))
                    select new 
                    {  
                         memberid = members.member_id
                         name = members.member_name
                         amount = members.membertransactionamount
                    };

Explanation:

If name_param is null or empty, the first part of the where condition will evaluate to true; if not, will do the comparison.

If the tran_param is null, the second part of the where will evaluate to true; if not, will do the comparison with membertransactionmaount.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜