Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

adding a variable column name to linq query

Posted on 2011-03-23
6
Medium Priority
?
693 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

650 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