Solved

adding a variable column name to linq query

Posted on 2011-03-23
6
680 Views
Last Modified: 2012-05-11
Hi,

I've been trying to get the below query to work with a variable column name, can anyone help me out please?

the "where tbl.columnname.Trim().Length > 0" part was my way of making it ignore cells with a value of 0 or null..
string columnname = "thecolname";

var results = from tbl in query
                          where tbl.columnname.Trim().Length > 0
                          group tbl by tbl.columnname into g
                          select new { var_text = g.Key, var_count = g.Count() };

Open in new window

0
Comment
Question by:OTM
  • 2
  • 2
  • 2
6 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 35199040
Did you try it through the datacolumn ?


var results = from tbl in query
                          where tbl(columnName).ToString().Trim().Length > 0
                          group tbl by tbl(columnName) into g
                          select new { var_text = g.Key, var_count = g.Count() };
0
 

Author Comment

by:OTM
ID: 35199197
Thanks dhaest, but it does't work, i get an error on the 2nd line of the query.

the query would basically create a list like this when being used for a gridview datasource:

var_text  var_count
type1      3
type2      5
type3      2

the table being read from would be this:
type1    
type1
type1
type2
type2
type2
type2
type2
type3
type3
0
 
LVL 2

Expert Comment

by:ericpeckham
ID: 35219462
Sorry, but I don't think you can use Linq to Sql that way.  It generates the Sql at compile time, so just as you can't do this in direct Sql, you can't do it with Linq.

You're stuck with using direct SQL, which the following page describes how to use in a Linq to Sql environment:  http://msdn.microsoft.com/en-us/library/bb399403.aspx
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:OTM
ID: 35253447
ok thanks guys,

i've decided to just use an if statement to specify different queries based on a parameter.. works fine but lots more lines of code.. i was sure this was possible
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 35253668
I finally had some time to adjust the query.  In code below I just create a simple datatable with 2 columns and fill them with data. Afterwards, I just pass the columnname for which I want to group

And here it works :)

result of first query:
+            [0]      { var_text = "type1", var_count = 2 }      <Anonymous Type>
+            [1]      { var_text = "type2", var_count = 4 }      <Anonymous Type>
+            [2]      { var_text = "type3", var_count = 1 }      <Anonymous Type>

Result of second query:
+            [0]      { var_text = "type6", var_count = 2 }      <Anonymous Type>
+            [1]      { var_text = "type5", var_count = 4 }      <Anonymous Type>
+            [2]      { var_text = "type4", var_count = 1 }      <Anonymous Type>

DataTable query = new DataTable("query");
            query.Columns.Add("col1");
            query.Columns.Add("col2");
            DataRow row;
            for (int i = 0; i<2;i++)
            {
                row = query.NewRow();
                row["col1"]="type1";
                row["col2"] = "type6";
                query.Rows.Add(row);
            }
            for (int i = 0; i<4;i++)
            {
                row = query.NewRow();
                row["col1"]="type2";
                row["col2"] = "type5";
                query.Rows.Add(row);
            }
            for (int i = 0; i<1;i++)
            {
                row = query.NewRow();
                row["col1"]="type3";
                row["col2"] = "type4";
                query.Rows.Add(row);
            }

            string columnName = "col1";

            var results = from tbl in query.AsEnumerable()
                          where tbl[columnName].ToString().Trim().Length > 0
                          group tbl by tbl[columnName] into g
                          select new { var_text = g.Key, var_count = g.Count() };

            columnName = "col2";

            results = from tbl in query.AsEnumerable()
                          where tbl[columnName].ToString().Trim().Length > 0
                          group tbl by tbl[columnName] into g
                          select new { var_text = g.Key, var_count = g.Count() };

Open in new window

0
 
LVL 2

Expert Comment

by:ericpeckham
ID: 35260533
That's impressive, Dhaest.  I guess I was stuck in "Linq to Sql" mode, thinking that it wouldn't work when querying a Sql database.  I still don't think it would, but as OTM may not be querying a Sql database, then you should win the prize!  I'd be really interested to know if this is possible with Linq to Sql.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question