Solved

Best way to select specific columns in a LINQ query

Posted on 2011-09-12
8
343 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:Mydeen Yussouf
[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
  • 5
  • 3
8 Comments
 
LVL 75

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:Mydeen Yussouf
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 75

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 1

Author Comment

by:Mydeen Yussouf
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
 
LVL 75

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 75

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:Mydeen Yussouf
ID: 36531298
@Kaufmed, did you try to compile the code?

Or is it meant to be pseudocode?
0
 
LVL 75

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

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

717 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