Solved

Linq Intersection

Posted on 2014-10-24
6
284 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

630 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