Solved

Filter data object using LINQ?

Posted on 2016-08-18
5
60 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 32

Expert Comment

by:it_saige
Comment Utility
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
Comment Utility
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 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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
Comment Utility
You....your good you!!

Thanks
JK
0
 

Author Closing Comment

by:jknj72
Comment Utility
thanks again
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now