Linq - Create A Concatenated String From A List Of Objects In 3.5?
Apr 27, 2012
I figure I should use 'Aggregate' but apparently I am getting it wrong First I get a list of my Entity objects Dim employers As List(Of myEntity) = (New XXXX()).getZZZ(userName, userType) Then I figured this would be a way to put all the names in a string Dim names as String = employers.Aggregate(Function(current, [next]) current.Name & " " & [next].Name)
I'm creating a text box with auto-suggested. So, it works well. It suggests only first name, but I want it to suggest the full name (first and last name which two different columns). Take a look at the following code behind that worked as expected :
I have a list of objects, each with 2 relevant properties: "ID" and "Name". Lets call the list "lstOutcomes".I need to check the list for duplicates (meaning object1.ID = object2.ID, etc.) and set a flag (valid = false, or something) if there is at least one duplicate. Also, it would be nice to send a message to the user mentioning the "Name" of the object, when it fails.I am sure I will need to use the Group By operator to do this, but I am not used to doing that in LINQ, and the examples out there are just not helping me. This article seems to be close to what i need, but not quite and it's in C#.
Here is a starting stab at it... Dim duplist = _ (From o As objectType In lstOutcomes _
Dim l as IList(Of Foo) = (From dataRow As DataRow In table.Select() Where CStr(dataRow("Column1")) = "A" Select New Foo(CStr(dataRow("Column1")), _ CStr(dataRow("Column2")))).ToList()
What's happening is that if i set a break-point to the constructor of Foo and step, the constructor is hit and the parameters are loaded with the arguments. However, l has empty Foo objects (the members in every object are Nothing). What could be happening here?
Dim objectsList as List(Of Object) = GetAllObjects() ' Filter from Objects just Persons ' Dim peopleList as List(Of Person) = ???
What is the most efficient and effective LINQ expression to do it?
EDIT
1 Dim selectedObjects As List(Of Object) = GetAllObjects()
2 Dim selectedPeople As IEnumerable(Of Person)= selectedObjects.OfType(Of Person)
3 Dim people As List(Of Person) = selectedPeople.ToList()
Error on 3:
Value of type 'System.Collections.Generic.List(Of System.Collections.Generic.IEnumerable(Of Person))' cannot be converted to 'System.Collections.Generic.List(Of Person)'.
Basically, I am trying to write a LINQ to Objects statement where the relationship is a grandparent, parent, child relationship. (You could also call it a Master Detail relationship.)
In Legacy code here is a simplified version what I am trying to accomplish. Dim coverages As New List(Of Coverage) Dim coverage As Coverage For Each rl In oClaimsPolicy.RiskLocations coverage = New Coverage coverage.Level = "Location" [Code] .....
If is it not clear one Location can have many Items and one Item can have many Coverages. I basically want to list the items and show the relationship between grandparent (Location), parent (Item) and child (Coverage).
I am having so trouble with the ToUpper() procedure with strings. I am using LINQ to find unique values in a list of objects. In that whole process I set the values to compare to all lowercase in order to get a proper list. My goal is to populate a dropdown list with the values. I'm attempting to set the first letter of of the string to uppercase and keep the rest lowercase however the toUpper() procedure is not working?
Private Function FixCase(ByVal strIn) As String Dim strOutput As String Dim intStringLength As Integer = strIn.Length - 1 strOutput = strIn.Substring(0, 1)
Elementos = From b In Elementos Where b.Value.IdGrupo = 0 Select b
"Elementos" is a dictionary(of long, MyObject). MyObject has approximately 50 properties.
The problem I have is that I have a new requirement to accept string parameter with a "where" condition. Something like "property1>10 and property2 like 'anystring' or property3<=25". (That means any property could have a condition. I have string and numeric properties.)
I'd like to keep using linq, adding the condition as it comes. I'm aware that it could produce an exception if the condition is misspelled or something, but that's acceptable (by try catchs). I wouldn't want to parse the string to build parameters or anything.
I don't know if this is doable, maybe with Linq, but I have a List(Of MyType):
Public Class MyType Property key As Char Property description As String End Class
And I want to create a Dictionary(Of Char, MyType) using the key field as the dictionary keys and the values in the List as the dictionary values, with something like:
New Dictionary(Of Char, MyType)(??)
Even if this is doable, internally it will loop through all the List items, I guess?
I would like to create list of child objects from list of parent object. Like If i have list of bookingroom which has one member room then i would like to create list of room from it.
eg. code:
Dim BookingRoomList As List(Of BookingRoom) = New List(Of BookingRoom) Dim RoomList As List(Of Room) = New List(Of Room) BookingRoomList = BookingRooms.FillGrid()
[Code]....
Is there any short cut method instead of iterating over for earch?
How can I get a new distinct list from an existing list using LINQ? This is what I have so far and it is not distinct but does give me a new list.
Dim tmpQryColumn = (From a In _allAudits Select New CheckBoxListItem With {.Id = a.AuditColumn, .Name = a.AuditColumn} ).Distinct() _columnList = New List(Of CheckBoxListItem)(tmpQryColumn)
I have to ask my first question at some point, so here she goes.I am trying to create a dynamic list of objects. The objects will have several types (String, Integer) associated with them. Currently I am forming a Collection as I instantiate new objects.Is there a prefered to map this collection to, say, a Datagridview? Will I have to Iterate through the collection and add the rows at each iteration?Would I be better served using an ArrayList or something else over a Collection?
I need two seperate lists, which every item is Integer, String, Bitmap - and one which every item is Integer, String String. However I don't know how to do this, or even where to look - I've googled for custom objects and custom object lists. What I'm trying to do is this.Custom Object1 is Integer, String, Bitmap Custom Object2 is Integer, String, String
In one thread I'll be adding items to List1(Of Object1), and processing them, and adding the results to List2(Of Object2), however I need to be able from other threads to look at the list and say only give me the items where Integer = (my thread ID), is this possible? Any help, or even links to information that would be relevant to this request would be helpful?
I am relatively new to C# and winforms, although I have been using Java for years, so forgive me if im asking something relatively straightforward!I have a DataGridView, it needs to update regularly so I wish to provide a Collection of objects as its DataSource and NOT a database table.So far I have a List<MyObject> which will be constantly updated via a BackgroundWorker thread. I want my DataGridView to update when a change has been made to the List i.e. if the objects are resorted, added to or deleted from etc.Currently my DataGridView only displays the first object I add to the List, if I add more the DataGridView doesn't update to display the new objects added.This is a basic overview of how im coding it:
private List<MyObject> myList= new List<MyObject>(); myDataGridView.DataSource = myList; Then my BackgroundWorker thread updates the list, e.g.
I'm suppose to end up with a label box with my name spelled out in it using string manipulation. This is suppose to happen using the concatenated string to output the name into the label box. I don't have any error codes but I also don't have my name in the label either. [code] "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. [code]
For my application, there is a login form in which users have the option for it to "save the login information" Kind of like MSN or other websites in which you go to a login page an the information is filled out. On my login form, I am using this code for my checkbox which is to "save login info"
Private Sub CheckBox1_CheckChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.Click Dim s As New System.Text.StringBuilder
I am writing a tool that logs stats for a first person shooter game. The game writes its information (including who killed who with which weapon) to a log file, which I read every so many seconds and parse so that I can write that information to a database.I am now having trouble parsing the weapon out of that string of information.
Some additional information is required: a weapon in this game can contain attachments (such as a scope, a grenade launcher, etc). Weapons can have either 1 or 2 attachments. Some weapons however cannot have any attachments at all.
Each log file entry that describes a kill contains a code that describes the weapon that was used. This code is a concatenation of either 3 or 4 parts:
<weapon>_<attachment>_mp <weapon>_<attachment1>_<attachment2>_mp where <weapon> is a code that describes a weapon and <attachment> is another code describing the attachment.This should be easy to parse by just splitting along the underscore characters, but there's a few catches:
1. Some attachments are able to kill players as well. Specifically: grenade launchers, flame throwers and underbarrel shotguns. In this case, the attachment is listed before the weapon:
<attachment>_<weapon>_mp Note also that in this case there is always only 1 attachment.
2. The biggest catch: some weapon names have an underscore in them (some even have 3 underscores)! So simply splitting along the underscore won't work in all cases; if the weapon name contains an underscore I'm splitting the name of the weapon..This makes the list of possible combinations a lot longer. The ones I can think of (I think these are all):
I'm writing a program that reads a directory of xml files (not well-formed), parses the xml, and dumps the data into an Excel Workbook.The file contents are similar in that they have many common xml nodes/attributes.Some files have additional nodes/attributes.The xml in these files represent configuration settings for a Web Application, one file per user.The expected output is a worksheet with a column for each node/attribute combination and a row of node/attribute values for each user.I'm just about finished with the program, but I've run into a bit of a snag.As the final step in the process, I need to loop through a generic list of array objects (that represent a user row) and send the array elements to Excel.I don't know the correct syntax to get the array objects out of the list and have been unable to find sample code specific to generic lists of array objects.[code]
I have a dictionary that I want to access with a key that is the combination of a string (AcctNum) and a date (BalDate).It seems to me the simplest approach is to create the key by simply converting the date to a string and concatenating:MyKey = BalDate.ToString & "|" & AcctNum
I know I also have the option of creating a composite key by writing a separate class and overriding GetHashCode() and Equals() a la this solution. To me, the concatenated string is a simpler, if somewhat less elegant, solution. Am I missing some compelling reason why I should go with the composite key class approach?
This lookup is the crux of the project I am working on, so performance is my main objective (with readability a close second).
Is there a way to dynamically create an object using a string as the class name?I've been off VB for several years now, but to solve a problem in another language, I'm forced to develop a wrapper in this one. I have a factory method to dynamically create and return an object of a type based on input from elsewhere. The provided input is meant to be the class name from which to create an object from. Normal syntax means that the entire class has to be explicitly spelled out. To do it this way, there could literally be hundreds of if/then's or cases to handle all the available class/object choices within the referenced libs:
If c_name = "Button" then obj = new System.Windows.Forms.Button If c_name = "Form" then obj = new System.Windows.Forms.Form ....
<WebMethod()> _ Public Function GetUsers(ByVal prefixText As String, ByVal count As Integer) As String() Dim UsersList As New List(Of String)
[code]....
However, This returns all the users from my user table. I want to get just the users where the fullname is like the prefixtext variable that is passed in to the function.Now I need to use LINQ(the only way I can think of). But how do I change my code.