r.hamd
r.hamd

Reputation: 264

how to show this linq query in a label or textbox using c#?

        var priv = from emp in re.Users
                   where emp.Name == struname
                   select new { emp.privilege };

for the privilege i have manager, officer and applicant when i press a button on the form i want to show the (priv) in a label or a textbox i have tried it does not work even when i convert it to string like label.Text= priv.ToString() but it still does not show me mananger, officer or applicant i dont know why can you help me please thanks in advance

Upvotes: 1

Views: 1408

Answers (3)

Alberto
Alberto

Reputation: 1863

@r.hamd I saw your auto-answer and I think that, although it works, is very confusing: as Binkan said you are creating and object while you need only a property.

I suggest this cleaner version:

Label.Text = re.Users.Single(u=> u.Name == struname).privilege;

Of course you have to try-catch this because if match fails you can't access privilege property.

One final note: properties should start with capital letter, therefore you should rename privilege to Privilege.

Upvotes: 2

r.hamd
r.hamd

Reputation: 264

thank you all very much for your contribution

i modified the code as the following and it worked properly for me

       var priv = (from emp in re.Users
                   where emp.Name == struname
                   select emp.privilege );
        Label.Text = priv.FirstOrDefault().ToString();

Upvotes: 1

Binkan Salaryman
Binkan Salaryman

Reputation: 3048

You are returning an anonymous type with select new { emp.privilege }. Maybe you meant select emp or select emp.privilege?

Upvotes: 0

Related Questions