Comparing two lists using linq to sql?

There are two linq methods that fit your exact needs: IEnumerable.Any() IEnumerable.All() Edit: You need to select a "Projection" of the "Ids" in the collection to get an enumerable collection of them, then you can use any / all on those (you can't compare complex types without an explicit comparison object) Here is a trivial example, I hope it helps List skills = new List( Enumerable. Range( 0, 20 ). Select( n => Guid.NewGuid() ) ); List canidateSkills = new List ( Enumerable.

Range( 0, 10 ). Select( c => new CanidateSkill() { CanidateId = Guid.NewGuid(), SkillId = skills. ElementAt( c ) } ) ); List desiredSkills = new List ( Enumerable.

Range( 5, 15 ). Select( d => new JobDesiredSkill() { JobId = Guid.NewGuid(), SkillId = skills. ElementAt( d ) } ) ); var anyDesiredSkills = canidateSkills.

Any( c => desiredSkills. Select( ds => ds. SkillId ).

Contains( c. SkillId ) ); // true var allDesiredSkills = canidateSkills. All( c => desiredSkills.

Select( ds => ds. SkillId ). Contains( c.

SkillId ) ); // false (Note: I also edited your question to update the sample classes used in this code, hope you don't mind. ).

There are two linq methods that fit your exact needs: IEnumerable.Any() IEnumerable.All() Edit: You need to select a "Projection" of the "Ids" in the collection to get an enumerable collection of them, then you can use any / all on those (you can't compare complex types without an explicit comparison object). Here is a trivial example, I hope it helps. List skills = new List( Enumerable.

Range( 0, 20 ). Select( n => Guid.NewGuid() ) ); List canidateSkills = new List ( Enumerable. Range( 0, 10 ).

Select( c => new CanidateSkill() { CanidateId = Guid.NewGuid(), SkillId = skills. ElementAt( c ) } ) ); List desiredSkills = new List ( Enumerable. Range( 5, 15 ).

Select( d => new JobDesiredSkill() { JobId = Guid.NewGuid(), SkillId = skills. ElementAt( d ) } ) ); var anyDesiredSkills = canidateSkills. Any( c => desiredSkills.

Select( ds => ds. SkillId ). Contains( c.

SkillId ) ); // true var allDesiredSkills = canidateSkills. All( c => desiredSkills. Select( ds => ds.

SkillId ). Contains( c. SkillId ) ); // false (Note: I also edited your question to update the sample classes used in this code, hope you don't mind.).

You can't use All to test if all items from one sequence are in another - you can only use All to test is all items in a sequence match a given predicate. I could be wrong though - example usage of 2) in this scenario? – RPM1984 Jul 11 at 23:42 2 @RPM you just have to construct the right predicate - eg JobDesiredSkills.

All(j => CandidateSkills. Contains(j)) – Jon Jul 11 at 23:48 @RPM a. All( n => b.

Contains( n ) );? – Brandon Moretz Jul 11 at 23:48 @Jon haha. Nice timing.

– Brandon Moretz Jul 11 at 23:48 Lol, i've been owned, forgot about contains. Nice. :) – RPM1984 Jul 11 at 23:50.

I don't have Visual Studio at handy right now but it should be something like: 1. JobDesiredSkills. Where(j=>CandidateSkills.

Any(c=>c. SkillId == j. SkillId)==true).ToList().Count()>0 2.

JobDesiredSkills. Where(j=>CandidateSkills. Where(c=>c.

SkillId == j. SkillId).ToList().Count()>0).ToList().Count()==JobDesiredSkills.Count().

This is actually what I tried originally, but I get this error: Unable to create a constant value of type 'JobDesiredSkill'. Only primitive types ('such as Int32, String, and Guid') are supported in this context. – Prabhu Jul 12 at 3:26.

The C# 3.0 specification defines a Query Expression Pattern along with translation rules from a LINQ expression to an expression in a subset of C# 3.0 without LINQ expressions. The translation thus defined is actually un-typed, which, in addition to lambda expressions being interpretable as either delegates or expression trees, allows for a great degree of flexibility for libraries wishing to expose parts of their interface as LINQ expression clauses. For example, LINQ to Objects works on IEnumerables and with delegates, whereas LINQ to SQL makes use of the expression trees.

The expression trees are at the core of the LINQ extensibility mechanism, by which LINQ can be adapted for many data sources. The expression trees are handed over to LINQ Providers, which are data source-specific implementations that adapt the LINQ queries to be used with the data source. If they choose so, the LINQ Providers analyze the expression trees contained in a query in order to generate essential pieces needed for the execution of a query.

This can be SQL fragments or any other completely different representation of code as further manipulatable data. LINQ comes with LINQ Providers for in-memory object collections, SQL Server databases, ADO.NET datasets and XML documents. The LINQ to Objects provider is used for in-memory collections, using the local query execution engine of LINQ.

The code generated by this provider refers to the implementation of the standard query operators as defined on the Sequence pattern and allows IEnumerable collections to be queried locally. Current implementation of LINQ to Objects uses e.g. O(n) linear search for simple lookups, and is not optimised for complex queries. The LINQ to XML provider converts an XML document to a collection of XElement objects, which are then queried against using the local execution engine that is provided as a part of the implementation of the standard query operator.

The LINQ to SQL provider allows LINQ to be used to query SQL Server databases, including SQL Server Compact databases. Since SQL Server data may reside on a remote server, and because SQL Server has its own query engine, LINQ to SQL does not use the query engine of LINQ. Instead, it converts a LINQ query to a SQL query that is then sent to SQL Server for processing.

8 However, since SQL Server stores the data as relational data and LINQ works with data encapsulated in objects, the two representations must be mapped to one another. For this reason, LINQ to SQL also defines a mapping framework. The mapping is done by defining classes that correspond to the tables in the database, and containing all or a subset of the columns in the table as data members.

9 The correspondence, along with other relational model attributes such as primary keys, are specified using LINQ to SQL-defined attributes. This class definition maps to a table named Customers and the two data members correspond to two columns. The classes must be defined before LINQ to SQL can be used.

Visual Studio 2008 includes a mapping designer that can be used to create the mapping between the data schemas in the object as well as the relational domain. It can automatically create the corresponding classes from a database schema, as well as allow manual editing to create a different view by using only a subset of the tables or columns in a table. The mapping is implemented by the DataContext that takes a connection string to the server, and can be used to generate a Table where T is the type to which the database table will be mapped.

The Table encapsulates the data in the table, and implements the IQueryable interface, so that the expression tree is created, which the LINQ to SQL provider handles. It converts the query into T-SQL and retrieves the result set from the database server. Since the processing happens at the database server, local methods, which are not defined as a part of the lambda expressions representing the predicates, cannot be used.

However, it can use the stored procedures on the server.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions