Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Linq Intersection

Posted on 2014-10-24
6
Medium Priority
?
299 Views
Last Modified: 2016-02-15
Quick question

I have a list

List<System.Guid> guidList;

Open in new window


I have a DB with a table that has a GUID field.

I want a list of items where the guid is on the list.

Would you use any, join, intersect?
0
Comment
Question by:infinidem
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 34

Expert Comment

by:it_saige
ID: 40402374
If we are talking about a list that contains guids then a simple where clause will suffice:
using System;
using System.Collections.Generic;
using System.Linq;

static class LinqExample
{
	private static List<TestData> DataElements = new List<TestData>();

	public static void Main()
	{
		Guid guid1 = Guid.NewGuid();
		Guid guid2 = Guid.Empty;
		Guid guid3 = Guid.NewGuid();

		for (int i = 0; i < 30; i++)
		{
			if (i % 3 == 0)
				DataElements.Add(new TestData(guid3, string.Format("Data element {0} for guid 3.", (from de in DataElements where de.Guid == guid3 select de).Count() + 1)));
			else if (i % 2 == 0)
			{
				if (guid2 == Guid.Empty)
					guid2 = Guid.NewGuid();

				DataElements.Add(new TestData(guid2, string.Format("Data element {0} for guid 2", (from de in DataElements where de.Guid == guid2 select de).Count() + 1)));
			}
			else
				DataElements.Add(new TestData(guid1, string.Format("Data element {0} for guid 1.", (from de in DataElements where de.Guid == guid1 select de).Count() + 1)));
		}

		Console.WriteLine("Data elements for Guid 1");
		Console.WriteLine("------------------------");
		foreach (var de in (from de in DataElements where de.Guid == guid1 select de))
			Console.WriteLine(de);

		Console.WriteLine(string.Empty);
		Console.WriteLine("Data elements for Guid 2");
		Console.WriteLine("------------------------");
		foreach (var de in (from de in DataElements where de.Guid == guid2 select de))
			Console.WriteLine(de);

		Console.WriteLine(string.Empty);
		Console.WriteLine("Data elements for Guid 3");
		Console.WriteLine("------------------------");
		foreach (var de in (from de in DataElements where de.Guid == guid3 select de))
			Console.WriteLine(de);

		Console.ReadLine();
	}
}

class TestData
{
	private Guid _guid = Guid.Empty;
	private string _data = string.Empty;

	public Guid Guid
	{
		get { return _guid; }
	}

	public string Data
	{
		get { return _data; }
	}

	private TestData() { ;}

	public TestData(Guid guid, string data)
	{
		_guid = guid;
		_data = data;
	}

	public override string ToString()
	{
		return string.Format("{0} - {1}", _data, _guid);
	}
}

Open in new window

Produces the following output:Capture.JPG
-saige-
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40402471
Hi infinidem;

Seeming that you have a List<Guid> in your program you can let Linq return those Guid's that are in your database that match your List. Here is a sample Linq query

// Your initialized list of Guid's
List<System.Guid> guidList;

List<System.Guid> results = (from g in db.YourTableName
                             where guidList.Contains(g.UniqueidentifierColumn)
                             select g.UniqueidentifierColumn).ToList();

Open in new window

0
 

Author Comment

by:infinidem
ID: 40402628
Would that be the same as:

var results = db.YourTableName.Where( abc => guidList.Contains(abc.UniqueIdentifierColumn));

Open in new window


I should add the results in this case would be to delete the items that match.

var results = db.YourTableName.Where( abc => guidList.Contains(abc.UniqueIdentifierColumn));

foreach (var result in results)
{
     db.DeleteObject(result);
 }

db.SaveChanges();

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40402694
Hi ;

To your question, "Would that be the same as:", if you are looking for a List<YourTableName> records then yes.

To your statement, "I should add the results in this case would be to delete the items that match.", yes the results of the query which are records that match the Guid's that are in the guidList will be deleted from the database.
0
 

Author Comment

by:infinidem
ID: 40402936
is there any code efficency that can be added to the delete?  Doing the foreach loop seems excessive.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40403021
None that I can think of. All it is doing is setting a flag to mark it for deletion.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question