Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linq Intersection

Posted on 2014-10-24
6
Medium Priority
?
317 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 35

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
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!

 
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

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.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

886 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