473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

FindAll. Linq.

Hello,

I have two lists, A and B. Both are lists of an object named Obj which
has two properties: ID and Name.

I need to check if all items in A exist in B by comparing their names.

If yes, then return true. If not then return false.

Could someone, please, help me creating this?

I tried the following but it is not working:

bool FoundAll = (from b in B
join a in A on b.Name equals a.Name
select b).Any();

Thanks,
Miguel
Aug 25 '08 #1
4 2650
What about this?

bool result = list1.All(
i =((from i2 in list2 where i2.ID == i.ID select i2).Count() 0)
);

Aug 25 '08 #2
shapper wrote:
I have two lists, A and B. Both are lists of an object named Obj which
has two properties: ID and Name.

I need to check if all items in A exist in B by comparing their names.

If yes, then return true. If not then return false.

Could someone, please, help me creating this?

I tried the following but it is not working:

bool FoundAll = (from b in B
join a in A on b.Name equals a.Name
select b).Any();
I think you want
bool foundAll = A.All(t =B.Contains(t, new MyComparer()));
where you need to implement
class MyComparer : IEqualityComparer<Obj>
{
public bool Equals(Obj x, Obj y)
{
return x.Name == y.Name;
}

public int GetHashCode(Obj obj)
{
return obj.Name.GetHashCode();
}
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 25 '08 #3
On Aug 25, 2:18*pm, Martin Honnen <mahotr...@yahoo.dewrote:
shapper wrote:
I have two lists, A and B. Both are lists of an object named Obj which
has two properties: ID and Name.
I need to check if all items in A exist in B by comparing their names.
If yes, then return true. If not then return false.
Could someone, please, help me creating this?
I tried the following but it is not working:
bool FoundAll = (from b in B
* * * * * * * * * * * * *join a in A on b.Name equals a.Name
* * * * * * * * * * * * *select b).Any();

I think you want
* *bool foundAll = A.All(t =B.Contains(t, new MyComparer()));
where you need to implement
* *class MyComparer : IEqualityComparer<Obj>
* *{
* * * * *public bool Equals(Obj x, Obj y)
* * * * *{
* * * * * * *return x.Name == y.Name;
* * * * *}

* * * * *public int GetHashCode(Obj obj)
* * * * *{
* * * * * * *return obj.Name.GetHashCode();
* * * * *}
* * }
--

* * * * Martin Honnen --- MVP XML
* * * *http://JavaScript.FAQTs.com/
You mean:

using System;
using System.Collections;

class MyComparer : IEqualityComparer<Object{
public bool Equals(Object x, Object y) {
return x.Name == y.Name;
}

public int GetHashCode(Object obj) {
return obj.Name.GetHashCode();
}
}

I get the error:
The non-generic type 'System.Collections.IEqualityComparer' cannot be
used with type arguments

Any idea why?

Thanks,
Miguel
Aug 25 '08 #4
shapper <md*****@gmail.comwrote:

<snip>
You mean:

using System;
using System.Collections;

class MyComparer : IEqualityComparer<Object{
public bool Equals(Object x, Object y) {
return x.Name == y.Name;
}

public int GetHashCode(Object obj) {
return obj.Name.GetHashCode();
}
}

I get the error:
The non-generic type 'System.Collections.IEqualityComparer' cannot be
used with type arguments

Any idea why?
Yes, you've used System.Collections instead of
System.Collections.Generic.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 29 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Rockwell | last post by:
I am new to using C# generics and I am liking what I am finding. However the examples in online help are lacking. Can someone help me with the FindAll method of the generic List class? As I...
5
by: Dave | last post by:
Hi All C# ADSI samples that I run cause unspecified errors. If I translate them into VB.NET they run fine. Here's an example: public string getEmail(string LDAPPath, string username) {...
5
by: Mystilleef | last post by:
Hello, Is there a simple flag to set to allow overlapping matches for the findall() regular expression method? In other words, if a string contains five occurrences of the string pattern "cat",...
1
by: JerryB | last post by:
Hi, I have a string like this: invalidStr = "192.168.*.1" I want to be sure I don't get a * followed by a number, i.e. I want invalidStr to be invalid. So I do: numberAfterStar =...
3
by: DagoFlores | last post by:
Hi, let's take a look to this code that we already know.: List<stringdinosaurs = new List<string>(); dinosaurs.Add("Compsognathus"); dinosaurs.Add("Amargasaurus"); .... List<stringsublist =...
5
by: David Longnecker | last post by:
I'm working to create a base framework for our organization for web and client-side applications. The framework interfaces with several of our systems and provides the business and data layer...
7
by: Abhishek | last post by:
Hi I am using a generic list in my program to have a list of objects stored. I want to compare all the objects in this list with object of another class and find all the objects which meet the...
2
by: =?Utf-8?B?bWdvbnphbGVzMw==?= | last post by:
I have a List<tobject consisting of objects which in themselves consist of BindingListViews of objects. When I want to search for a object value I normally create a foreach loop and increment a...
1
by: Doug | last post by:
Hi, I have a collection of objects and I need to compare each item in the collection to see if it's a match on any or all of the others. I have stored my collection in an item like so: ...
2
by: Alexnb | last post by:
Okay, I am not sure if there is a better way of doing this than findAll() but that is how I am doing it right now. I am making an app that screen scapes dictionary.com for definitions. However, I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.