[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Filter data object using LINQ?

Posted on 2016-08-18
5
Medium Priority
?
139 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 35

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 35

Accepted Solution

by:
it_saige earned 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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 real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses

825 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