Solved

Linq Intersection

Posted on 2014-10-24
6
237 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 32

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 62

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 62

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 62

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now