C# - Linq Select Certain Properties Into Another Object?
May 28, 2009
So say I have a collection of Bloops
Class Bloop
Public FirstName
Public LastName
[Code]....
Is it possible using Linq to select the FirstName and LastName out of all the Bloops in the collection of Bloops and return a collection of Razzies? Or am i limited to a For-Loop to do my work?
To clear up any confusion, either VB or C# will do. Also this will probably lead to me asking the question of (What about using a "Where" clause).
I's there a way that I can access the properties of all controls stored into my array of object using LINQ.
Public Sub DisAble(Byval ParamArray ctrlCollection() As Object)End Sub That is my sub routine where im trying to reset all controls that will stored into that object of array. But im trying to attain that using LINQ so I wont use any loops to access all of it.
I can't quite figure out why this Linq Statement isn't working as i would expect:
[Code]....
I would assume that this would create a new collection of anonymous types, that would be distinct. Instead it creates a collection the size of the "ThisParentCollection" with duplicate "MyAnonymousType" in it (duplicate id's).
I have done a lot of searching to what appears to be a simple LINQ problem but I can't figure out how to do grab an object out of a collection that has a specified minimum (or max value) without resorting to a sort like this[code]...
I am somewhat new to object oriented programming and am attempting to flatten a Linq object hierarchy by using a shim class.how to initalize a derived class with property values from a base class?I have two objects, a base object with about 100 properties, and a derived object which inherits from the base object and adds a few additional properties beyond those of the base object. My simple constructor creates the derived object, but I am looking for a way to initialize the derived object properties with values from the base object.Right now I am using reflection to iterate over the properties individually and I suspect there may be a better way. The following example shows my shim class constructor for the derived class, and two properties:
newProperty1 - a new string property of the derived class
flattenedProperty2 - a new string property of the derived class, copied from a 2nd-level object of the base class
Code example:
Public Class derivedObj Inherits baseObj Private _newProperty1 As String[code].......
Is this the correct constructor approach to flatten the object hierarchy using a shim class? My second question relates to initialization of properties in the derived class. The constructor above creates the derived object, but what is the best way to initialize the derived object properties with values from the base object? The following code uses reflection to iterate over the properties individually, but I suspect there may be a better way.
Code example:
' property names are in the string array fieldNames
'baseObjQuery is an ienumerable of baseObj
'derivedObjList is a list of derivedObj[code].....
Is there a simple way to initialize values for the properties in the derived object based upon the values of the common properties in the base object?
I want to build a MVC helper function for which I pass a Entity Framework object and have it build a select. Generally speaking I tried something like this:
Public Function RenderSelect(ByVal helper As HtmlHelper, ByVal sSelectName As String, ByVal aItmes As Array, Optional ByVal sTitleKeyName As String = "name", Optional ByVal sValueKeyName As String = "id") As String ' open select
[Code].....
However, it isn't working. It would be great to have a way to do this and pass the current entity object.
I am getting following error whenever I want to use IIf function inside entity framework query.
LINQ to Entities does not recognize the method 'System.Object IIf(Boolean, System.Object, System.Object)' method, and this method cannot be translated into a store expression.
Are the below two queries functionally the same?The first one doesn't return any data, but the second works fine with same exact input.Can someone point out what's wrong in my first query? [code]
I spent a lot of time on this problem. I am able to do simple Group By LINQ queries (on one property) but for multiple fields I'm a little stuck... Here is a LINQPad sample of what I want to do :
dim lFinal={new with {.Year=2010, .Month=6, .Value1=0, .Value2=0}, new with {.Year=2010, .Month=6, .Value1=2, .Value2=1}, new with {.Year=2010, .Month=7, .Value1=3, .Value2=4},
How can I convert an object of type System.Data.Linq.DataQuery to System.Linq.IQueryable?I'm working with Visual Basic/Silverlight, and the source code of my query is as follows:
Public Function Get_Cli_Pag() As IQueryable(Of V_Cliente_Pagare) Dim Qry = From P In Me.Context.Pagares Join C In Me.Context.Codigos On C.Codigo
I'm learning LINQ and VB and just spent an hour trying to access the fields of an anonymous type defined in a linq query. The key (I learned) is to convert the query to a list before you try to iterate through it with a for loop. How to access property of anonymous type in C#?
This does not work: edit (this compiles, but intellisense does not recognize the type)[code]...
I'm having trouble getting a Linq to Sql query to work. Basically, I want the sum of a column in a table for only rows matching a certain condition. A simplified version is this:
Orders = order that contains products OrderProducts = relational table containing products that are in an order Products = table containing information about products
I can get the total sum of the qty of products like this:
Dim totalQty = requests.Sum(Function(p) p.OrderProducts.Sum(Function(q) CType(q.ProductQty, Nullable(Of Integer))))
(Requests is a IQueryable of Orders)But I also need to get the sum of qty where the actual product meets a certain condition. Something like...
Dim totalQty = requests.Sum(Function(p) p.OrderProducts.Sum(Function(q) CType(q.ProductQty, Nullable(Of Integer))))... WHERE p.OrderProducts.Product.ProductAttribute = true
How would I go about getting that query with the additional where clause to only get the sum of the qtys where the productAttribute = true ?
I have the following query which groups some records and then filters where the count of the grouped records is 1.
I'd like to take the returned result and perform another query to retrieve the entire record from the JobcodesWorkingRollup table where the ParentNode column equals the result of this query:
Dim query = From r In context.GetTable(Of JobcodesWorkingRollup)() _ Group r By r.ParentNode Into g = Group _ Where g.Count = 1 _ Select New With {.cnt = g.Count, .nm = g.FirstOrDefault.ParentNode}
I am trying to figure out how to add my own properties to an object. i am trying to make my own rpg game and i want to be able to refer to a integer property like so: strengthstat = bronzelongsword.attackstrength. of course strength stat being an integer vairable that would equal to the property attackstrength that belongs to the bronze longsword.
Why can't I get any values back when I use Linq to Sql? BHS_TimeSheet is my database table in which have some records. Model.TimeSheet is a class I create in the model. Private db As DataFactoryDataContext
Today I ran into an issue with LINQ to objects (not SQL) that popped up due to a typo. I had a .Select one place and a .Where in another place. I was expecting same result but they are showing different numbers. Assume somelist has 10 elements with all elements having qty = 0
I have seen different examples in Linq and one used Select and another didnt but both return the same results. Is there a reason why we should use Select ?
In my database I have 3 tables Act, ActVisi and Visi. Table ActVisi is an association table between Act and Visi tables. In My EF4 data model the association table ActVisi is replaced by a many to many navigation property in both Acts and Visis entities.I want to select the entries whose Visicode=1 (in the ActVisi association table). I have tried the query below and it works when I remove Include("Visis").Where(Function e.Code = 1) but I am getting all the results. How can I make the below query work. I suspect that my lambda syntax in vb is incorrect.
I am constantly receiving back objects and it would be very useful be able to print out all of their properties. Is there a way in VB to get all the properties of an object and print them out (to console)?
I'm debuging an complex calculation object in my project, and I'd like to show its various and many properties in a textbox, to make my tests easier.
Can I do something like
for each p as someKindOfProperty in MyObject1 debug.print(p.name & " - " & debug.print p.value) textbox1.text = textbox1.text & vbcrlf & p.name & " - " & p.value next
I need to get all the object properties of a web browser. in this web page how do i retireve all the proeprties, like the text box name, html id, button name and so on.Also help me with retrieveing all the html ids of the browser. the html ids could be seen in the source page. but how do i get those ids.
I have a property grid and I want to be able to set the selectedobject to a class. The class contains properties. This is what I have, but it 'OtherFiles' is not an object.
pgProperties.SelectedObject(OtherFiles)
How do you create a class as an object. In VB.NET 2008 please. C# is ok.
I have a form that has 100 text boxes with 100 coinciding labels. I want them hidden until another function populates the text boxes with data gathered elsewhere. The data could be between 1 and 100.
I would like one loop where I can assign the data to the correct text box, set it visible and set the label visible. I can not figure out how to do this in VB, or know if it is even possible.
Here is an example of what I would like to do
Dim i As Long = 1 Dim textbox As String = "textbox"
[code]....
I would also like to do this to hide all the items on the form in the on load event.
I'm a C# coder, and I have trouble even reading VB.NET, so please forgive what may turn out to be an incredibly dumb question. But I have some code which looks like this:
CODE:
So, ignoring the fact that the syntax is probably wrong - how does VB.NET get the Name property from sourceObject? Inspecting it at run-time reveals that sourceObject is of a type that supports a property called Name with a getter, but what does VB.NET do in this case? Is there some extra code that is being generated by the compiler to somehow cast this automagically at run-time?