Solved

Linq Intersection

Posted on 2014-10-24
6
261 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
  • 3
  • 2
6 Comments
 
LVL 33

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 63

Accepted Solution

by:
Fernando Soto earned 500 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
Independent Software Vendors: 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!

 
LVL 63

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 63

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

Technology Partners: 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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