4

I'm using LINQ to SQL in C# in my application. I need to be able to select a column of a row, depending upon a variable. This is easy for the row as it's a simple where clause, but I'm at a loss with only selecting a specific column. Here is my code so far:

var permissions = (from s in dc.Permissions where s.dashboardname == permission select s.[variablehere]).Single();

Is this easy to accomplish?

1

5 Answers 5

1

Is it possible to change your database structure so that your columns becomes rows? (Pivot your table?)

Eg.

Permissions Table
-----------------
Id
Dashboardname
Page1
Page2
Page3
...

and turn it into

Permissions Table
-----------------
Id
Dashboardname
Pagename

Then you could use the where clause to select the row you want?

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure that I understand your question completely, so this might be a bit off. But could your problem perhaps be solved by using the LINQ Dynamic Query Library?

You can use the DynamicQuery library against any LINQ data provider (including LINQ to SQL, LINQ to Objects, LINQ to XML, LINQ to Entities, LINQ to SharePoint, LINQ to TerraServer, etc). Instead of using language operators or type-safe lambda extension methods to construct your LINQ queries, the dynamic query library provides you with string based extension methods that you can pass any string expression into.

See the above link for samples and downloads.

Comments

0

I don't think this is either possible or a good practice. Note that if the variable name is provided by user he or she can easily take all the data they want. Maybe you should try using enumeration and switch() clause?

1 Comment

Thanks for your reply, the variable is not supplied by the user - the variable is just the pages name, and there is a column for each page.
0

Say the class that holds the permissions is named Permission, you can define an extension method:

public static class PermissionExtensions
{
    public static object SelectProperty(this Permission obj, string variable)
    {
        return obj.GetType().GetProperty(variable).GetValue(obj, null);
    }
}

You can use this in your query like this:

(from s in dc.Permissions where s.dashboardname == permission select s)
    .Single().SelectProperty(variable);

This doesn't select the property in the query but instead gets it from the instance.

Comments

0

Another answer that came in my mind is to create table with key-value pairs i.e.

  • dashboard
  • key
  • value

where primary key is (dashboard, key).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.