Solved

adding a variable column name to linq query

Posted on 2011-03-23
6
687 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
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!

 

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
parse string in c# 5 40
GET INFO ABOUT WHAT THE PRINTER IS DOING ? 11 69
history of pages 7 47
Why am I losing my session variable value 11 33
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

739 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