C# - .NET Generics - Compare Two Lists And Filter?

Jun 19, 2011

I have two generic lists of type T. Both lists contain same type, and I'd like to create a third list (or a filtered version of list 2) based on the items in list two that do not exist in List 1, based on the ID of each item.Each list holds a "Package" object, which has an ID property.Right now I mocked up the code using For Each loops, which I know is horrible (the Big O is constant time) so I'd like a more efficent method. this code is in VB per project requirments, but I prefer C# - so either code sample would work for me.

Private Sub RemoveStockPackagesFromSelection()
Dim p As Package
Dim packageList As List(Of Package) = New List(Of Package)

[code]....

View 5 Replies


ADVERTISEMENT

.net - Compare Different Generic Lists?

Mar 2, 2011

I have a method where I'm taking a generic object and using TypeOf to check what has been passed through. Now I would like to pass it a List(Of T) (T being anything) and no matter what T is I'd like to do the same thing to this list. I tried the following:

public sub foo(ByVal obj As Object)
if TypeOf obj Is List(Of Object) Then
'do stuff
end if
end sub

but this doesn't seem to work if I pass it List(Of String), say. I suppose that List(Of Object) and List(Of String) are being treated as different objects, but I thought that since String is an object, the comparision might work.

Now I realise that this is as ugly as sin and I'm better off overloading the method to take List(Of T) as its parameter, but I'm mostly asking out of curiosity: is there a way of comparing the types List(Of Object1) and List(Of Object2) and getting a positive result, since they are both just List(Of T)?

View 3 Replies

Compare Differences Between Lists?

Feb 17, 2011

Compare differences between lists?I have two sets of lists below[code]...

View 1 Replies

[2005] How To Compare Two Lists

Jan 22, 2009

Im trying to compare two lists,to find out how which items occur in both lists. I also want to test for duplication within a list.Now, the obvious way is a loop/compare which works fine for a low number of items. Beyond 10000 items, things slow down much more than a factor of 10.

Dim List1 As New List(Of String)
Dim List2 As New List(Of String)
Dim List3 As New List(Of String)

[code]....

View 3 Replies

Arrays - Compare Two Lists 2D And Determine

May 2, 2012

I declare my 2D lists:

Dim _invoiceitems As New List(Of List(Of String))
Dim _dbitems As New List(Of List(Of String))

Each List is filled like this: Example Code To fill:

_invoiceitems.Add(New List(Of String))
_invoiceitems(0).Add("Code #")
_invoiceitems(0).Add("Quantity")

Well, now i need a third list called (_changesitems) Note that this result with the differences: be the result of subtraction of quantities if this is found (dbitems - invoiceitems).

View 1 Replies

Comparison - 4 String Lists - Compare The Contents Of The Input List - Containing Words

May 31, 2009

I have 4 string lists as follows

Dim input_list As New List(Of String) Dim input_POS As New List(Of String) Dim trigger_list As New List(Of String) Dim trigger_POS As New List(Of String)

I want to compare the contents of the input list (containing words (e.g., want, the) etc. to the ones of the trigger list (containign again words) and if a same entry (i.e. word) get the POS for both input word and trigger word from the relevant lists and put them into a property list. For example

input list (want, the, right)trigger list(want, there, wait)input list POS (verb, article, adjective)trigger list POS (verb, pronoun, verb)

Output property list (want:verb, want:verb) (only want is common between input and trigger list). Then I need to conduct a test between the two property items for similarites. If they are exactly the same, the code should output a confirmation "i.e., Yes" into a textbox. If not the code should output a "No" plus the contents of the property list into a textbox.

View 6 Replies

Added A Textbox To Filter Words Out From Filename Compare?

May 17, 2009

i have added a textbox to filter words out from my filename compare, any way i keep getting

Error1'txtComWords' is not a member of 'WindowsApplication1.Form1.StringSift3'.

Public Function PrepareWord(ByVal strWord As String, ByVal bDisRomTags As Boolean, ByVal bDisComWords As Boolean) As String
Dim strToCheck As String = strWord
If bDisRomTags Then

[Code]...

View 2 Replies

Sorting 2 Lists Based On Only One Of The Lists?

Apr 1, 2011

I have two lists. One list is a list of names, the other list is a list of how many times each name is found in the first list noted in the database

so...
Nick
John
Jim
Jack

is the firs tlist

10
13
13
2

is the second list. Nick had 10, john 13 and so on.I want to sort the second list from large to small, but have the index for the first list still linked to the correct amount of calls.i do it this way so I can

for x = 0 to num_of_names
string = lst_name(x) & "-" & lst_count(x)
next x

View 3 Replies

Know If An Array Of Lists Contains Similar Items (similar Lists)?

May 20, 2010

I know how to check whether an item exists in a list using (MyList.Contains), But I do not know how to check the whole list. For example (use one button and one richtextbox):

[Code]...

View 14 Replies

Compare Date - Compare Textbox1 And Textbox2 Text

Mar 3, 2010

I have two textbox in my application.

Textbox1.Text="19-Jan-2010"

Textbox2.Text="Jan 2010"

May I know how can I compare that Textbox1 and Textbox2 text is within same month and same year?

View 1 Replies

IFF Is Not Null Query - Filter A Sql Db With Many Filter Criteria Which Inert The User In Textboxes

Feb 15, 2012

I m trying to filter a sql db with many filter criteria which inert the user in textboxes .. i ve tryed this query

[Code]...

View 5 Replies

VS 2008 : Filter The Results Of A Datagridviw Using The Bindingsource.filter Method?

Mar 20, 2009

I am trying to filter the results of a datagridviw using the bindingsource.filter method.

Dim filters(2) As String
...
If Me.txtPartIDFilter.Text = "" Then

[code]....

The issue is in the last line. ID_Part is an Int64 data type filters(0) is a string so the system returns a "Can't perform 'Like' operation on System.Int64 and System.String" error message.As you can see I tried converting the data type, but that failed since it is encased in String.Format()- it's going to be a string no matter what, I suppose.It's almost 1:00 a.m. so I'm not firing on all cylinders.

View 2 Replies

Binding Source.filter - Filter My Access Database

Oct 10, 2011

I'm trying to filter my access database and when I "search" by name, it works, but when I search by policy number it doesn't seem to work.

Here's my code:

If RadioButton3.Checked = True Then
Form3.Show()
Me.Close()

[CODE]...

View 11 Replies

Databinding.filter - Filter Date And Also Time (shortime)

Sep 20, 2010

i want to filter date and also time (shortime) for example if Datum.hour="8:15" andalso Datum.day of week "Saturday"or "Monday" now i can only filter date ... this is my filterline objbindingsource.Filter = "Datum > = '" & Dtp1.Value.Date & "'"

View 5 Replies

Datagridview - Use Filter With LIKE Clause When The Filter Text Contains %?

Feb 13, 2012

I want to filter a text in all columns in a datagridview. So I use Like % in the filter text. However, I found that the text(strFilter in my code) may contain % sometimes. So it have a syntax error. How can I use filter with like clause when the filter text contains %?

Private Sub PassFilter(ByRef dataTable As DataTable, ByVal strFilter As String)
Try
Dim dataview As DataView = dataTable.DefaultView

[Code].....

View 2 Replies

Sql Server - Filter Row And Put It In A Textbox And Filter Another Row In Another Text Box?

Mar 16, 2012

i want to filter row and put it in a textbox and filter another row in another text box vb.

ex.
ID--------- Date -------- msg_num ------------ Message <<
10001 -- 01/01/2012 ------ msg1 ------------- Blah! Blah![code].....

View 1 Replies

Can The T Of Generics Be An Interface

Jul 17, 2010

In my program I have an interface iGraphable that contains two properties: Abscissa and Ordinate. Then I have an xxxx class (actually more than one) implementing iGraphable and a ListOfxxxx class implementing BindingListView(Of xxxx).To draw graphs I have a Graph class with a property called Data whose type is BindingListView(of iGraphable).Why have I a cast exception when I pass a BindingListView(Of xxxx) to the Data property.

View 3 Replies

Constraints On Generics With .Net?

Dec 14, 2011

I have the a function that is declared like so: Public Sub Modify(Of SIMType As {New, DAOBase})(ByVal obj As SIMType)

I also have a class called Products which is declared like so:

Public Class Products Inherits DAOBase

So as you can see, if I were to call this function like so:

Modify(Of Products)(new Products())

This would not be an issue. The issue actually arises when I try to cast the object being past in to its real type. For example: both do not work. I get a Value of type SIMTYPE cannot be converted to IMS.Products error. Im assuming this is because I am using generics. Is there a way to adjust my function to allow for a casting operation like I am trying to do? In the end, what I need is a reference of the actual type (Products in this case) to the object.

View 2 Replies

.net - Polymorphism In .Net When Dealing With Generics?

Jan 6, 2012

I have a function called Modify. It is delcared like so:Public Function Modify(Of SIMType As {New, DAOBase})(ByVal obj As DAOBase) As Boolean

You can see that this function is generic. It takes as a paramer a object that is a DAOBase or subclasses of DAOBase.Inside the modify function there is a call like so:

DAOToGP(obj)This is where the polymorphism comes into play. There are four or so subclasses I have created of DAOBase. I have written a DAOToGP() for each of these types. So in the Modify() function, when it calls the DAOToGP(obj), polymorphism should kick in and it should call the correct implementation of DAOToGP() depending on the type that I pass into Modify().

However, I get the following error:Error 20 Overload resolution failed because no accessible 'DAOToGP' can be called without a narrowing conversion:'Public Shared Function DAOToGP(distributor As Distributors) As Microsoft.Dynamics.GP.Vendor': Argument matching parameter 'distributor' narrows from 'SierraLib.DAOBase' to 'IMS.Distributors'.'Public Shared Function DAOToGP(product As Products) As Microsoft.Dynamics.GP.SalesItem': Argument matching parameter 'product' narrows from 'SierraLib.DAOBase' to 'IMS.Products'. C:Usersdvargo.SIERRAWOWIRESDocumentsVisual Studio 2010ProjectsSIMDev_2SIMIMSDVSIMLibGPGPSIMRunnerRunnersRunnerBase.vb 66 39 IMS

I am kind of at a loss here. I am not sure why it cant figure out which function to call.

View 2 Replies

.net - Suppress COM Generics Warning?

Dec 8, 2010

I'm compiling a VB.Net 2.0 app (created in VS2008) using msbuild, and now I've added a generic return type, it's giving me the following:

Warning: Type library exporter
encountered a generic type instance in
a signature. Generic code may not be
exported to COM.

Having just spent ages removing all of the previous warnings, I don't really want to add a new one. Any idea how to get rid of it (aside from not using generics)?I don't know what details I'd put in the attribute, or what number to put in the project-level ignore list.

View 1 Replies

.Net To Use Generics For Settings Persistance?

Nov 30, 2010

I'm trying to reduce code bloat, reduce errors and simplify codebehind by use of generics. In this case I'm applying generics to declaration of persistable properties. Persistance is implemented by My.Settings. Here's the code so far.

[Code]...

View 1 Replies

C# - Appropriate To Use Generics Versus Inheritance?

Apr 28, 2009

What are the situations and their associated benefits of using Generics over Inheritance and vice-versa, and how should they be best combined?I'm going to try to state the motivation for this question as best I can:I have a class as shown below:

[Code]...

Now suppose I have a repository that takes an InformationReturn argument, that has to strore different fields in a DB depending on the type of Info object T is. Is it better to create different repositories each for the type T is; one repository that uses reflection to determine the type; or is there a better way using inheritance capabilities over/with generics?

View 6 Replies

C# - Multiple Generics Ambiguity?

Dec 27, 2011

The codes below are exactly the same, except that one is C# and the other one is VB.Net.C# compiles just fine, but VB.Net throws the warning:

Interface 'System.IObserver(Of Foo)' is ambiguous with another
implemented interface 'System.IObserver(Of Bar)' due to the 'In' and
'Out' parameters in 'Interface IObserver(Of In T)'

Why does VB.Net show the warning and not C#? And most important, how can I resolve this problem?

Obs: I'm using .Net Framework 4 with Visual Studio 2010 Ultimate.

VB.Net Code:

Module Module1
Sub Main()[code]......

View 2 Replies

Call NET Generics In A Dynamic Way?

Oct 28, 2011

Names of entities have been altered to protect their identities...

I've created a class called AnimalSearch(Of AnimalType As Animal(Of Int32))

Public Class AnimalSearch(Of AnimalType As Animal(Of Int32))[code]...

View 2 Replies

Calling Constructor When Using Generics

Mar 9, 2011

I'm not sure if this is possible or not. I have a number of different classes that implement interface IBar, and have constructors that take a couple of values. Rather than create a bunch of almost identical method, is it possible to have a generic method that will create the appropriate constructor?

private function GetFoo(Of T)(byval p1, byval p2) as List(Of IBar)
dim list as new List(Of IBar)
dim foo as T
' a loop here for different values of x
foo = new T(x,p1)
list.Add(foo)
' end of loop
return list
end function

I get:
'New' cannot be used on a type parameter that does not have a 'New' constraint.

View 2 Replies

Converting VB6 Collections To .Net Generics?

Apr 21, 2012

I have a VB6 project with about 100 custom collection classes which I want to convert to VB.Net. A typical example would be something like.

Class CAccounts
Private m_Accounts As New Collection
Public Sub Add(newItem As CAccount)
m_Accounts.Add newItem, newItem.IdKey

[code].....

All of the collection classes in the project use this standard approach. However, not all the properties/methods of the collection classes are actually used. Most of the collection are used in "for each" loops. Keyed access using the string key is quite common. Keyed access by index is much less common.

Ideally I'd like to take a standard approach to converting these classes. I don't really want to have to review each collection and it's usage to consider whether I need a List, Dictionary, etc. Some of these collection contain 100,000 objects, and some will only contain 10. However, on the other hand I don't want to cause performance problems by using a more complex structure where a simpler option would do.

Sticking with the old style Collection. So, it would be relatively easy to convert to VB.Net But, I'd rather move to the more modern structures.Have CAccounts Inherit KeyedCollection(Of String, CAccount). Fortunately most of the classes held in the collections do have the key as part of the class (eg CAccount.IdKey above). This seems to work well. However, relatively few classes will access the colelction by numeric index. So, perhaps this is overkill if I only want keyed access by the string key?Have CAccounts Inherit Dictionary(Of String, CAccount) for the classes where I don't need access by numeric index. The problem I have with this is that all the existing "for each" loops are like "for each account in accounts". I don't want to have to change all these occurences to something like "for each account in accounts.Values". Although perhaps I can get round this by changing the default property?Have CAccounts Inherit MyCollection(Of String, CAccount), where MyCollection is my own bespoke collection. This seems a bit too much hard work.

View 2 Replies

Generics And Duck-Typing XML In .NET?

Feb 12, 2010

I'm working with some XML representations of data instances.I'm deserializing the objects using .NET serialization but something in my soul is disturbed by having to write classes to represent the XML.[code]

View 4 Replies

Generics Versus Extensions?

Nov 8, 2009

This is my problem:

<System.Runtime.CompilerServices.Extension()> _
Function CastAs(Of TSource As TTarget, TTarget)(ByVal array As TSource()) As TTarget()
Return Global.System.Array.ConvertAll(array, Function(src As TSource) DirectCast(src, TTarget))
End Function

View 10 Replies

Have Generics In A Class Property?

Jul 28, 2010

I have a Class that is defined as

Public MustInherit Class Entity(Of T As Entity(Of T))

And various classes derived from it. I would like to have another class accept all of the derived objects as a property, but I cannot seeem to find a workable syntax. If it were a parameter of a sub, I could say

Public Sub Foo(T As Entity(Of T))(TheEntity As T)

I can't seem to get that to work for a property:

Public Property E(Of Entity(Of T))() As Entity(Of T)

Gives me "Type parameters cannot be specified on this declaration"

Public Property E() As Entity2008(Of T)

Gives me "Type Parameter T does not inherit from or implement the constraint type ..."Is there an acceptable way to do this or am I stuck with using a Set Sub and a Get Function?

View 2 Replies

Numeric Type Only Generics?

Jan 20, 2011

Suppose I have an interface called IParseable(Of TParsed, TUnparsed) which requires two functions:[code]Is there a way that I can restrict TParsed and TUnparsed to be numeric types (for which operations like "*" and "+" are already defined)?The problem is that, when I try to implement my interface and define one of the functions, e.g.:[code]VS throws an error saying the "*" is not defined for TUnparsed. I understand that, since TUnparsed could be anything, but is there a way to restrict my generic such that, say, TUnparsed could only be Double, Integer, Long, etc? To require Control to be a TextBox (or maybe I don't understand that very well either). But, anyway, any idea or am I way off track? Just trying to get a hang of these interface thingies and generic types.

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved