1

I am using Linq in C#. I have defined my own function like this

public static void AutoSuggest(TextBox t, string columnName, string tableName)
{

}
  • t is the refrence of the textbox
  • ColumnName is the Name of specific column
  • Tablename is the name of specific table

In this user defined function, using the parameters, I want to get the specified column data from the specified table of the database.

So, how should I generate this query in Linq?

3
  • Please, first try yourself.....If you get error,then specify the error.... Commented Mar 12, 2012 at 10:53
  • which specific row is the value in? Commented Mar 12, 2012 at 10:54
  • Have you made a database model? Commented Mar 12, 2012 at 11:00

1 Answer 1

4

If you wish to use LINQ, you would be better talking about a source and selector; some IQueryable<T> and something like Expression<Func<T,string>>. If you want to use column/table names, firstly they must be white-listed (don't ever accept the names from, say, a web-request), but the code would be simply:

var values = dataContext.ExecuteQuery<string>("select distinct [" + columnName
      + "] from [" + tableName +"]").ToList();

this is a pretty naive approach, but it shows basic usage; then you would data-bind as normal. However, I'd be much more inclined for the calling code just to do, for example:

var values = dataContext.SomeTable.Select(x => x.SomeProperty)
              .Distinct().ToList();

which would avoid risk of injection etc.

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

1 Comment

@LukeMcGregor you can't use parameters for the table/column names

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.