Link to home
Start Free TrialLog in
Avatar of jknj72
jknj72

asked on

Filter data object using LINQ?

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!!!
Avatar of it_saige
it_saige
Flag of United States of America image

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 -User generated imageYou 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-
Avatar of jknj72
jknj72

ASKER

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!!!
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jknj72

ASKER

You....your good you!!

Thanks
JK
Avatar of jknj72

ASKER

thanks again