Solved

Best way to select specific columns in a LINQ query

Posted on 2011-09-12
8
337 Views
Last Modified: 2012-05-12

Being new to LINQ - In the code below how to select specific columns?

DataTable ResultView = LookUp(); //datatable has 3 columns

var query = (from lookup in ResultView.AsEnumerable()
            where lookup[2].ToString() == "cond" //this works
                select lookup[0], lookup[1] //??? Need only the first two columns
            );
0
Comment
Question by:olmuser
  • 5
  • 3
8 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36526672
You would want to use the new keyword. You can pick an arbitrary name for each column:
var query = from lookup in ResultView.AsEnumerable()
            where lookup[2].ToString() == "cond" //this works
            select new
            {
                FirstColumn = lookup[0],
                SecondColumn = lookup[1] //??? Need only the first two columns
            };

// Use like this:
foreach (var item in query)
{
    Console.WriteLine(item.FirstColumn.ToString());
    Console.WriteLine(item.SecondColumn.ToString());
}

Open in new window

0
 
LVL 1

Author Comment

by:olmuser
ID: 36526770
Oh I forgot to mention, so here are the additional details and I am adding extra points.

I need to get the first two columns and as many rows as there are to a datatable. Is there a way to do that without using foreach in one single step...
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36526783
Is there a way to do that without using foreach in one single step...
What is it you want to do with the data? Remember, declaring the query itself (lines 1 to 7 in my last post) don't actually execute the query; the query is executed, in my example, in the foreach line. You can return a list of these columns if you like (negating the use of a foreach):

var items = query.ToList();

Open in new window

0
 
LVL 1

Author Comment

by:olmuser
ID: 36526855
I would like to store the resulting dataset in another datatable array

I would like to be able to return viewschemas[index] which is a Datatable.

I think I could do this now by(contd from above in pseudo code) :

Datatable x = new Datatable();
foreach(var item in query)
{
   x.addrow(query.datarow);
}

viewSchema[indx] = x;
return viewSchema[index];

It may look twisted but I am trying to use existing code as part of maintenance.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 36527137
Well, I've managed to track down a method of doing what you seek via Linq; however, it would necessitate adding a bit of code. If you take the code from #1 and #2 at the following:

    http://msdn.microsoft.com/en-us/library/bb669096(v=VS.90).aspx

you can subsequently do something like this:

var query = from lookup in ResultView.AsEnumerable()
            where lookup[2].ToString() == "cond" //this works
            select new
            {
                Column1 = lookup[0],
                Column2 = lookup[1]
            };

DataTable x = query.CopyToDataTable();

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36527140
P.S.

The following using statements are required for the code at that link:

using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;

Open in new window

0
 
LVL 1

Author Comment

by:olmuser
ID: 36531298
@Kaufmed, did you try to compile the code?

Or is it meant to be pseudocode?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36531350
did you try to compile the code?
Did you add the code that I referred to in the link? The code I posted is not the entire solution--you need the code from the link as well. Just #1 and #2, though.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Default parameter problem in C# 3 27
Library to convert HTML to PDF. 8 44
comparing string in C# 3 33
Achieve json result 2 28
Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now