Solved

Filter data object using LINQ?

Posted on 2016-08-18
5
82 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
  • 3
  • 2
5 Comments
 
LVL 33

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 33

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

778 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