whats going on below. Can someone write a good linq to return the logic below
Below I am trying to determine the volunteer driver that will take neighbor to preferred zip codes.
Below is list of volunteers, Each volunteers has there preferred zipcodes to drive to.
The output or return dataset should only contain the volunteers that will drive to preferred zipcodes they will driver
the neighbor to. If a neighbor choose a zipcode that a volunteer do not prefer, that volunteer will not be in the return dataset.
| Volunteer Preferred Zipcodes to drive to | |||
| Mike | Sam | David | Kelvin |
| 11111 | 11111 | 11111 | 11111 |
| 22222 | 22222 | 22222 | |
| 33333 | 33333 | 33333 | 33333 |
| 44444 | 44444 | ||
| 55555 | 55555 | 55555 | |
| Contacts wants only the volunteers that will take them to the zipcodes below |
| Neighbor- Jordan |
| 55555 |
| 44444 |
The turn result set should only return Sam and Kelvin
David SmithPosted Jul 22, 2012, 10:49 PM
VulpesPosted Jul 22, 2012, 6:34 PM
using System;
using System.Collections.Generic;
using System.Linq;
class Volunteer
{
public string Name {get; set;}
public List
public Volunteer(string name, List
{
Name = name;
ZipCodes = zipCodes;
}
}
class Program
{
static void Main()
{
var mike = new Volunteer("Mike", new List
var sam = new Volunteer("Sam", new List
var david = new Volunteer("David", new List
var kelvin = new Volunteer("Kelvin", new List
var volunteers = new List
string neighbor = "Jordan";
string[] neighborZipCodes = {"33333", "11111", "22222"};
var temp = neighborZipCodes.OrderBy(z => z); // make sure sorted
var query = volunteers.Where(v => temp.Intersect(v.ZipCodes).OrderBy(z => z).SequenceEqual(temp));
Console.WriteLine("The following volunteers can accept {0}'s zipcodes\n", neighbor);
foreach(Volunteer v in query) Console.WriteLine(v.Name);
Console.ReadKey();
}
}
The output should be:
The following volunteers can accept Jordan's zipcodes
Mike
Sam
David
EDIT
David SmithPosted Jul 22, 2012, 5:18 PM
is there another way to do the logic below in red
string neighbor = "Jordan";
string zipCode1 = "44444";
string zipCode2 = "55555";
var query = volunteers.Where(v => v.ZipCodes.Contains(zipCode1) && v.ZipCodes.Contains(zipCode2));
Console.WriteLine("The following volunteers can take {0} to {1} and {2}\n", neighbor, zipCode1, zipCode2);
foreach(Volunteer v in query) Console.WriteLine(v.Name);
Console.ReadKey();
}
A volunteer will can have infinite amount of zipcodes to drive to or infinite amount zipcodes he choose not to drive to, but in the dataset it will contain only the zipcodes that the driver prefer to drive to only,
I'm trying to come up with some dynamic linq that will basically return a dataset with the all qualified drivers. In the case above sam and kelvin are qualified becuase they prefer to drive to 44444, 55555 zipcodes.
can we take out the and clause out of the link, because there will be more than two zipcodes, we may have to use a foreach, Im now for sure how to go about this yet
VulpesPosted Jul 22, 2012, 3:43 PM
However, if we ignore that and just look at volunteers who can do both 44444 and 55555, then this code should work:
using System;
using System.Collections.Generic;
using System.Linq;
class Volunteer
{
public string Name {get; set;}
public List
public Volunteer(string name, List
{
Name = name;
ZipCodes = zipCodes;
}
}
class Program
{
static void Main()
{
var mike = new Volunteer("Mike", new List
var sam = new Volunteer("Sam", new List
var david = new Volunteer("David", new List
var kelvin = new Volunteer("Kelvin", new List
var volunteers = new List
string neighbor = "Jordan";
string zipCode1 = "44444";
string zipCode2 = "55555";
var query = volunteers.Where(v => v.ZipCodes.Contains(zipCode1) && v.ZipCodes.Contains(zipCode2));
Console.WriteLine("The following volunteers can take {0} to {1} and {2}\n", neighbor, zipCode1, zipCode2);
foreach(Volunteer v in query) Console.WriteLine(v.Name);
Console.ReadKey();
}
}