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

x
?
Solved

Getting Data From Linq

Posted on 2014-03-21
14
Medium Priority
?
201 Views
Last Modified: 2016-02-10
Given a Linq query, is there a way to get data from a specific row and column ?

Like..

myCell = Query.RowIndex(4).ColumnIndex(6)
0
Comment
Question by:Dodsworth
[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
  • 7
  • 5
  • 2
14 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39945549
It would helpful to know the structure of the data source that you are querying. Generally speaking, there is no concept of "row" and "column" in LINQ. You simply have an IEnumerable of objects that have properties. The closest concept to "row" and "column" would be that the row is the sequence number (or index) of the record within the IEnumerable, and the column would be the specific property. Properties can be defined in any order within a class, so it would be difficult to know the ordering of how they were selected by the query simply by looking at the class definition.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39945682
Hi Dodsworth;

Not that simple but it can be done. I am going to assume that the data source of the Linq query is a DataTable object and the Linq query assigns the results to the variable c and c is of type List<DataRow>. Now because the DataRow's in the list do not have a RowIndex property you will need to find that out.

// Getting the RowIndex of a DataRow first get the table it is assigned to
DataTable table = c[index In List].Table;
// Get the RowIndex of the DataRow in list c
int rowIndex = table.Rows.IndexOf(results[index In List])
// Select the column index you want and get cell value, assuming index 2
var myCell = table.Rows[rowIndex].ItemArray[2];

Open in new window

0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39945708
I have the "row" issue sorted.  Is there a way to get a property value at a given property index ?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39945807
What business goal are you trying to achieve? Perhaps there is a better, more stable design that could accommodate what you are after.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39945844
Hi Dodsworth;

You still have not indicated weather my assumption is correct so in answer to your last  post the value of the cell can be found from the last line in my last post.
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39955700
I don't think DataTable is available in the framework that I'm using.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39955880
What is the data type of the variable query in your original question.
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39955980
Query is the Linq definition.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39955995
The variable query is of some data type. If you do not know what the data type is please post the actual code with declarations so that we can determine what is going on.
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39956120
Sorry to confuse.  Looking back, I could have worded it better!  There is no actual code.

myCell = query.RowIndex(4).ColumnIndex(6)

was to give an idea of what I wanted to achieve, where query could be

Dim query = From d in dc.Items
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39956162
Then this question becomes difficult to answer because Linq has many dialects. Just as a spoken language may have many dialects for example Spanish is spoken in Spain, Puerto Rico, Mexico, South American countries and they all have words that mean one thing in one dialect of the language but does not have the same meaning in another dialects of the language. For example In Linq to Entity Framework and Linq to SQL you can't use the ToString method call within the query where as in Linq to Object it is fine to do so.
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39956994
Ok lets backtrack to Kaufmed's comment .
What business goal are you trying to achieve?

I have an XAML page with UI elements bound to a Linq derived DataContext that allows the user to Next/Previous through the query results and modify the data where necessary.

The problem is that some of the controls need to be a ListPicker (like a combobox) control, so the user can select from a range of valid entries when they want to change the data.

As the ListPicker is bound to a lookup datasource, I need a method to 'discover' the value from the page of the current value to set the ListPicker to the correct value when the user is just browsing rather than changing the value.
0
 
LVL 1

Accepted Solution

by:
Dodsworth earned 0 total points
ID: 40025820
I solved it using

o.GetType.GetProperty(col.column).GetValue(o, Nothing)

Open in new window


where o is the data object from the page.
0
 
LVL 1

Author Closing Comment

by:Dodsworth
ID: 40036628
solved it
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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