开发者

join using lambda expressions and retrieving the data

开发者 https://www.devze.com 2023-04-04 19:57 出处:网络
I have a members table with columns member_Id, member_Lastname, member_Firstname, member_Postcode, member_Reference,

I have a members table with columns

  member_Id,
  member_Lastname,
  member_Firstname,
  member_Postcode,
  member_Reference,
  member_CardNum,

and i have another table mshipoptions with columns

mshipoption_id
mshiptype_id

and i have another table mshiptypes

 mshiptype_id
 mshiptype_name

another table memtomship

 memtomship_id
 mshipoption_id
 member_id

and my entity name is eclipse

at the form load i am filling the datagrid view by using the below method....

private void reportmembers()
{
        MemberControlHelper.Fillmembershiptypes(cbGEMembershiptype); 
        var membersreport = from tsgentity in eclipse.members
                                join memtomships in eclipse.mem开发者_如何学编程bertomships on tsgentity.member_Id equals memtomships.member_Id
                                join mshipoptiions in eclipse.mshipoptions on memtomships.mshipOption_Id equals mshipoptiions.mshipOption_Id
                                join mshiptypes in eclipse.mshiptypes on mshipoptiions.mshipType_Id equals mshiptypes.mshipType_Id
                                select 
                                new {                                     
                                    tsgentity.member_Id,
                                    tsgentity.member_Lastname,
                                    tsgentity.member_Firstname,
                                    tsgentity.member_Postcode,
                                    tsgentity.member_Reference,
                                    tsgentity.member_CardNum,
                                   mshiptypes.mshipType_Name,                                   
                                };

           if (txtfirstname.Text != "")
           {
              dgvmembersrep.DataSource = membersreport.Where(t => t.member_Firstname == txtlastname.Text).ToList();                
           }
           if (txtcardnum.Text != "")
           { 
                dgvmembersrep.DataSource = membersreport.Where(a => a.member_CardNum == txtcardnum.Text).ToList(); 
           }
}

that was fine,...

My problem is here , i have one comboboxsay (cbgemembershiptype)......

when ever the user select the membership type in (cbgemembershiptype) i want retrieve the details of members those who have that membership type....


(Your question is unclear in terms of whether the query you've got already does what you want. I'm assuming it does.)

It's unclear why you'd want to convert this query into one using lambda expressions. It's certainly possible, but each join would introduce a new range variable - by the end, the strictly literal translation would end up with a select involving something like

member_Id = a.b.c.member_Id

... it wouldn't be very readable at all.

There are ways of improving it if you're writing it out by hand, but it still wouldn't be as clear as the query expression.

You should definitely know both forms, and write using whichever form is clearest for the query in question - which in this case is definitely the query expression form.

For more information about how query expressions are translated, see my Edulinq post on that topic.

0

精彩评论

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