How can I use the value of a variable with LINQ?


I'm trying to assign a variable to a select using LINQ.

My function which makes it evaluate a data set to get the SourceID according to the caption that has been passed into the select.

What I try to do is from an application in Windows Forms. The user when entering the value of this caption in a textBox, this value can be replaced by the existing value of the LINQ.

Here is my function:

private string GetMappingTable()
    string SourceID = "";

    var ds = GetMappingTable();
    foreach (DataTable dst in ds.Tables)
        foreach (DataRow dr in dst.Rows)
            var DataSourceId = ds.Tables["Table"]
            .Select("Caption = 'testUSers'")
            .Select(r => r["SourceID"]) 
            .Where(s => s != DBNull.Value)
            .Select(s => s.ToString()) 

            SourceID = DataSourceId;
    return SourceID;

I want that where .Select("Caption = 'testUSers'") says the user from the texBox can enter another caption and that can be replaced.


In the select it says 'testUsers', if the user decides to put Users. This should be put in the value of the caption where it says testUsers.

How would you do that?

Thanks in advance.

asked by A arancibia 08.04.2016 в 16:32

2 answers


The first thing you can do is that the GetMappingTable () function receives a parameter, which would be the user's input.

private string GetMappingTable(string texto)

Then to replace the value within the function you can do it in the following way

.Select(String.Format("Caption = '{0}'",texto))

Or in this other way

.Select("Caption = '" + texto + "'")
answered by 08.04.2016 / 16:43

what you have to do is that instead of writing the testUSers by default, handle it as a String type variable within the GetMappingTable () method, and in the .Select method concatenate that variable as follows: .Select ("Caption = '" + text + "'")

This way you will get a change in the "text" every time you modify the content of your variable. Greetings

answered by 24.05.2016 в 20:06