Solved

Filter data object using LINQ?

Posted on 2016-08-18
5
103 Views
Last Modified: 2016-08-23
I have a data object that I pass to my gridview user control as a generic list and I would like to just use the fields/properties that I am returning from the stored proc I am running. My object has about 25 properties and the proc I am running is only returning about 5-7 fields. I am currently running a prepareColumns to hide columns not needed but I would like to just return the fields I need and set them to my DataSource without having to worry about what to show and what not to?
My object is called workItem and my query returns these fields
WeekID
WeekRange
Signature
temCount  
TravelItemCount
ImageItemCount
But when I pass the object to the datasource it shows every property in my object so I would like to just send the object with these fields and not the unneeded fields...
Any help would be appreciated.....
THANKS!!!
0
Comment
Question by:jknj72
[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
  • 3
  • 2
5 Comments
 
LVL 34

Expert Comment

by:it_saige
ID: 41761501
You can always use an anonymous type; e.g. -

Default.aspx -
<%@ Page Title="Home Page" Language="VB" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="EE_Q28964203._Default" %>
<form id="form1" runat="server">
	<asp:GridView ID="GridView1" runat="server">
	</asp:GridView>
</form>

Open in new window

Default.aspx.vb -
Public Class _Default
	Inherits Page
	ReadOnly people As New List(Of Person)(From i In Enumerable.Range(0, 20) Select New Person() With {.ID = i, .FirstName = String.Format("Firstname{0}", i), .MiddleName = String.Format("Middlename{0}", i), .LastName = String.Format("Lastname{0}", i), .IsWorking = i Mod 2 = 0, .Birthdate = DateTime.Now.AddYears(-(9 * i + 1))})

	Protected Sub OnLoad(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
		GridView1.DataSource = (From p In people Select New With {p.ID, p.FullName, p.Age}).ToList()
		GridView1.DataBind()
	End Sub
End Class

Class Person
	Public Property ID() As Integer
	Public Property FirstName() As String
	Public Property MiddleName() As String
	Public Property LastName() As String
	Public Property Birthdate() As DateTime
	Public Property IsWorking() As Boolean

	Public ReadOnly Property FullName() As String
		Get
			Return String.Format("{0} {1}. {2}", FirstName, MiddleName.First(), LastName())
		End Get
	End Property

	Public ReadOnly Property Age() As String
		Get
			Return String.Format("{0} years old", DateTime.Now.Year - Birthdate.Year)
		End Get
	End Property
End Class

Open in new window

Default.aspx.designer.vb -
'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated. 
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict On
Option Explicit On


Partial Public Class _Default

	'''<summary>
	'''form1 control.
	'''</summary>
	'''<remarks>
	'''Auto-generated field.
	'''To modify move field declaration from designer file to code-behind file.
	'''</remarks>
	Protected WithEvents form1 As Global.System.Web.UI.HtmlControls.HtmlForm

	'''<summary>
	'''GridView1 control.
	'''</summary>
	'''<remarks>
	'''Auto-generated field.
	'''To modify move field declaration from designer file to code-behind file.
	'''</remarks>
	Protected WithEvents GridView1 As Global.System.Web.UI.WebControls.GridView
End Class

Open in new window

Produces the following output -Capture.JPGYou just want to make sure that if what you are showing needs to allow CRUD methods that you provide a way to connect the displayed row with it's associated datarow; i.e. - I would connect my data row by the id field.

-saige-
0
 

Author Comment

by:jknj72
ID: 41762360
Ok I was able to apply this filter to my DataSource which just has the fields I want so it works great but when I do the DataBind it throws an error

"Unable to cast object of type 'System.Collections.Generic.List`1[VB$AnonymousType_3`7[System.Int32,System.String,System.Int32,System.Int32,System.Int32,System.String,System.String]]' to type 'System.Collections.Generic.List`1[CRIB_EMS.BLL.WorkItem]'."

Now, I didnt put in the this piece of code but it looks like this is what I am missing?

ReadOnly people As New List(Of Person)(From i In Enumerable.Range(0, 20) Select New Person() With {.ID = i, .FirstName = String.Format("Firstname{0}", i), .MiddleName = String.Format("Middlename{0}", i), .LastName = String.Format("Lastname{0}", i), .IsWorking = i Mod 2 = 0, .Birthdate = DateTime.Now.AddYears(-(9 * i + 1))})

Im just a little confused on how I can create this for my object. I though this was for formatting the fields to display(ie, FullName and Birthdate) which I dont need to do. So If I want to just display the records how they are coming back, how would I do that?

Thanks again!!!
0
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 41762842
The ReadOnly people is a list of well-defined objects.  The datasource of the grid view is not, rather the datasource of the grid view is a list of objects based on a subset of properties of the well-defined objects.

As to the error, the anonymous type generated by the linq statement is not a list of WorkItem, just use the ID field in order to form the relationship.

-saige-
0
 

Author Comment

by:jknj72
ID: 41767656
You....your good you!!

Thanks
JK
0
 

Author Closing Comment

by:jknj72
ID: 41767657
thanks again
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

Title # Comments Views Activity
Consume a webservice via VB in Visual Studio 2015 3 42
PowerShell: ForEach-Object Export to CSV 4 89
Access-Control-Allow-Origin 4 41
asp.net repeater server posting 5 35
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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