LINQ Dictionary Non-Distinct Values - c#

I am struggeling a little with trying to write the LINQ query to do the following,
public class MyClass
{
public int ID {get; set;}
public int Name {get; set;}
public IEnumerable<string> SomeIEnumerable {get; set;}
}
I am trying to create the object using LINQ from a DB query that would return something as follows,
ID Name SomeString
0 NameA "MyValue"
1 NameB "Value1"
1 NameB "Value2"
2 NameC "Value"
The LINQ is pretty easy to write as well,
from DataRow row in dataSet.Tables[0].Rows
select new MyClass
{
ID = (int)row["ID"],
Name = row["Name"].ToString(),
SomeIEnumerable = new List<string>{ row["SomeString"].ToString() }
};
The tricky part is how do I turn this into a Dictionary where
dictionary[1].SomeIEnumerable = {"Value1", "Value2"}
A simple ToDictionary would throw an ArgumentException
The main issue here, is how do I handle the fact that the keyis not distinct, and be able to lookup in the temporary dictionary the existing value to add to it the values I am interested in.

You can also your grouping, and then use IGrouping to retrieve the list of items in the group
Sample code
var simulatedData = Enumerable.Range(0,10).Select(x=> new {Key=x%3+1, Index=x});
var dict = simulatedData.GroupBy(x=>x.Key).ToDictionary(x=>x.Key, x=>x.Select(t=>t.Index));

Instead of calling ToDictionary, call ToLookup. :)

Answer thanks to #Tilak is,
from DataRow row in dataSet.Tables[0].Rows
group row by new
{
ID = (int) row["ID"],
Name = row["Name].ToString()
} into rowGroup
select new MyClass
{
ID = rowGroup.Key.ID,
Name = rowGroup.Key.Name,
SomeIEnumerable =
from row in rowGroup
select row["SomeString"].ToString()
};

Related

LINQ - select from db and compare two lists

I am new to linq and I am writing a process which I believe can be improved.
To make it simple :
I get a list of objects.
I check in a db table which object has a row in the database.
I return a list of objects with aditional boolean exists/not exists.
I have the following simple POCO
public class Project
{
public Guid? Id {get; set;}
public string name {get; set;}
}
Notice not always I have an id and therefor I should skip this object.
(strange but this is only very close to my requirement in reallife)
Here is my Code - need improvement:
// Get List OF Project Guids
List<Project> ProjectList = GetProjects()
IEnumerable<Guid?> projectsIDs = from package in packages select package.Key;
List<Guid?> prjGuidsList = projectsIDs.ToList();
// Sends the list of Guids and return only the one that exists in the db
// will be implemented with select.. where.. in..
List<Guid?> dbProjects = FilterSharedVersions(prjGuidsList);
// create a new object that will contain the true false value
List<ProjectsToken> tokens = packages.Select(subject => new ProjectsToken
{
Id = subject.id
MetaKey = subject.Name,
exists = dbProjects.contains(subjecy.id)
}
).ToList();
return tokens;
The Contains method of the List class will perform very poorly in O(n). You need to build a HashSet instead, then the method Contains will perform in O(1).
var dbProjects = new HashSet<Guid?>(FilterSharedVersions(prjGuidsList));
var tokens = packages.Select(subject => new ProjectsToken
{
Id = subject.id
MetaKey = subject.Name,
exists = dbProjects.Contains(subjecy.id)
}
).ToList();

Copy one List to another List and SubList with LINQ

I have a list of employees, and all of them have another list nested which is called the EmployeeValuesCollection.
So my class is something like :-
public Employee(int employeeID, string jobTitle, int companyID,
List<EmployeeValues> employeeValuesCollection)
{
EmployeeID = employeeID;
JobTitle = jobTitle;
CompanyID = companyID;
EmployeeValuesCollection = employeeValuesCollection;
}
Now I wish to populate this object from another object with LINQ, and so far I have :-
List<DataFileRow> dataFiles = dfRow.Rows;
dataFiles
.ForEach(l => employeeList
.Add(new Employee(l.EmpID, l.JobTitle, l.CompID)));
That works however I do not know how to add the employeeValuesCollection in the statement. Is it possible to do?
So I was thinking something like :-
dataFiles
.ForEach(l => employeeList
.Add(new Employee(l.EmpID, l.JobTitle, l.CompID,
new List<EmployeeValuesCollection> .............)));
Thanks for your help and time.
It will be better to iterate through the input parameter employeeValuesCollection list inside the Employee constructor to create a local variable of type EmployeeValues and add it to the Employee class's instance list variable. If we use EmployeeValuesCollection = employeeValuesCollection;, we are actually assigning the reference. So if we modify some values of employeeValuesCollection, the same change will get reflected to EmployeeValuesCollection.
public Employee(int employeeID, string jobTitle, int companyID, List<EmployeeValues> employeeValuesCollection)
{
EmployeeID = employeeID;
JobTitle = jobTitle;
CompanyID = companyID;
foreach (var obj in employeeValuesCollection)
{
var empVal = new EmployeeValues() { Name = obj.Name};
EmployeeValuesCollection.Add(empVal);
}
And you can use the LINQ statement as
dataFiles.ForEach(l => employeeList.Add(new Employee(l.EmpID, l.JobTitle, l.CompID, l.EmployeeValuesCollection)));
You can use the Linq projection over each row. I'll do it in 'proper' linq:
employeeList = (from r in dfRow.Rows
select new Employee(r.EmpID, r.JobTitle, r.CompID,
new List<EmployeeValues>(/*parameter*/))).ToList();
Until you specify what EmployeeValues actually is (and which property on DataFileRow they're accessed from) it's difficult to say any more. But let's say it's just more properties, and I'll summise they are like key/value pairs (despite the name pluralisation on the type, that's a bit confusing).
I've taken the names here from your third comment on your question, and assumed that DataFileRow type exposes these values as an IEnumerable called EmployeeValues. Based on that - you can use an inner projection.
employeeList = (from r in dfRow.Rows
select new Employee(r.EmpID, r.JobTitle, r.CompID,
new List<EmployeeValue>((from v in r.EmployeeValues
select new EmployeeValues(v.title, v.value, v.isVisible))).ToList());

Update One List of Objects Attribute with Other List using LINQ

I have a scenario as think
class a
{
String Username;
String val;
}
List<a> lst = new List<a>();
List<a> lstnew = new List<a>();
What i required is to that in lstnew i have some updated values in val Attribute (Only in Several Objects) , what i required is to update the lst with updated values in lstnew as the Username Attribute using LINQ
You can join the two lists on UserName, and then update the Values in the first list with those in the second.
For example, given this class and lists:
public class a
{
public string UserName { get; set; }
public string Value { get; set; }
}
List<a> list = new List<a>
{
new a { UserName = "Perry", Value = "A" },
new a { UserName = "Ferb", Value = "B" },
new a { UserName = "Phineas", Value = "C" }
};
List<a> newList = new List<a>
{
new a { UserName = "Phineas", Value = "X" },
new a { UserName = "Ferb", Value = "Y" },
new a { UserName = "Candace", Value = "Z" }
};
You can join to get the elements with common UserNames:
var common = from a1 in list
join a2 in newList on a1.UserName equals a2.UserName
select new { A1 = a1, A2 = a2 };
At this point, if I understand you correctly, you want to update the elements from the original list:
foreach(var c in common)
{
c.A1.Value = c.A2.Value;
}
at which point the elements in list look like:
UserName Value
-----------------
Perry A
Ferb Y
Phineas X
It sounds like you have two lists. One of which is named lst and contains a full list of usernames and a second one named lstnew that contains a list of usernames who have had their val property updated. I suggest unioning the untouched usernames with the ones that have been updated. This represents the most LINQ-friendly solution I can think of.
var updatedList = Enumerable.Union(
lst.Where(x => !lstnew.Any(y => y.Username == x.Username)),
lstnew).ToList();
you should be able to use the .Zip() method to execute this.
lst.Zip(lstNew, (orig, new) => {
orig.Username = new.Username;
return orig;
});
the idea that you are getting each pair together, then instead of returning a new one, changing the orig.Username value and return the orig.
This should also do the trick. Zip method, propsed by Alastair Pitts assumes that both collections have the same order of elements and each element from first list has correspondent element in second list. My approach is more generic, it simply looks for corresponding element by comparing Username property. Still it assumes that for each element in lstNew there is corresponding element in lst.
lstNew.ForEach(new => lst.First(orig => orig.Username == new.Username).val = new.val);
I know this is an old question but a more elegant solution that I have developed, which is a slight improvement over the one given by #JeffOgata would be:
var newList= lst.GroupJoin(lstnew ,
i => i.UserName ,
j => j.UserName ,
(i, j) => j.FirstOrDefault()?? i );
Where lst is the original list and lstnew is the new list.
This will just replace the entire object in the first list with the corresponding object in the second list (the join) if one exists.
It is a slight improvement over the answer given by #JeffOgata
The result is the same.
If you have complex objects then iterating through each object then going through all the properties was a problem, simply replacing the old object with the new one was quicker.
This hopefully will help someone.

Modify Dictionary Value is possible.What is the right approach?

I have dictionary that is populated and I have no control of.
I need to modify the value how can I do that?
I have put a noddy example together to explain the problem
class Program
{
static void Main(string[] args)
{
Dictionary<Customer, int> CustomerOrderDictionary = new Dictionary<Customer, int>();
CustomerOrderDictionary.Add(new Customer { Id = 1, FullName = "Jo Bloogs" },3);
CustomerOrderDictionary.Add(new Customer { Id = 2, FullName = "Rob Smith" },5);
//now I decide to increase the quantity but cannot do the below as value has no setter
foreach (var pair in CustomerOrderDictionary)
{
if(pair.Key.Id==1)
{
pair.Value = 4;///ERROR HERE
}
}
}
}
public class Customer
{
public int Id { get; set; }
public string FullName { get; set; }
}
Any suggestions?
Thanks a lot
I suggest you work out which keys need modifying first, and then iterate over those modifications. Otherwise you'll end up modifying a collection while you're iterating over it, which will throw an exception. So for example:
// The ToList() call here is important, so that we evaluate all of the query
// *before* we start modifying the dictionary
var keysToModify = CustomerOrderDictionary.Keys
.Where(k => k.Id == 1)
.ToList();
foreach (var key in keysToModify)
{
CustomerOrderDictionary[key] = 4;
}
The problem here is that pair is typed to KeyValuePair which is a readonly object and can't be modified. Additionally the KeyValuePair collection is a way of viewing the contents of the dictionary (not changing it).
What you want to do here is just modify the dictionary directly. The Key in the KeyValuePair can be used to update the same entry in the dictionary.
if(pair.Key.Id==1) {
CustomerOrderDictionary[pair.Key] = 4;
}
EDIT
As Jon pointed out the assignment will invalidate the iterator. The simplest, but ineffecient route, is to copy the enumerator at the start of the loop.
foreach (var pair in CustomerOrderDictionary.ToList())
Here is an alternate approach
1) Create a new class
// wrapper class to allow me to edit a dictionary
public class IntWrapper
{
public int OrderCount{ get; set; }
}
2) Change this declaration
Dictionary<Customer, IntWrapper> CustomerOrderDictionary = new Dictionary<Customer, IntWrapper>();
3) Assign your variable
pair.Value.OrderCount = 4;
foreach (Customer customer in customers.Keys)
{
if ( customer.Id == 1 )
customers[ customer ] = 4;
}
CustomerOrderDictionary[1] = 4;
Here's one way to do that (just the assigning a value part..):
CustomerOrderDictionary[new Customer { Id = 1, FullName = "Jo Bloogs" }]=4
Notice that "1" is not a key in your dictionary. a Customer is, so you'll have to use that.
Notice also that Customer Should implement IEquatable as explained here
Ok, in your example you're effectively just finding the entry for the Customer object with Id = 1 and updating the associated value. In practice, I think that your code will likely be able to obtain a reference to your intended Customer object prior to updating the associated value in the dictionary. If that is the case, then there's no need for a loop.
Below is a very simple example where a loop is not needed because your code already has a reference to the customer1 variable. While my example is overly simplified, the concept is that you could potentially obtain a reference to your desired Customer object through some means other than iterating over the dictionary.
static void Main(string[] args)
{
Dictionary<Customer, int> CustomerOrderDictionary = new Dictionary<Customer, int>();
Customer customer1 = new Customer { Id = 1, FullName = "Jo Bloogs" };
Customer customer2 = new Customer { Id = 2, FullName = "Rob Smith" };
CustomerOrderDictionary.Add(customer1, 3);
CustomerOrderDictionary.Add(customer2, 5);
// you already have a reference to customer1, so just use the accessor on the dictionary to update the value
CustomerOrderDictionary[customer1]++;
}
If you need to perform some kind of update on multiple Customer objects based on some other criteria, then you might need a loop. The following example assumes that you'll have some collection other than the dictionary that stores your Customer objects, and that you can use that collection of Customer objects to identify the ones whose associated value in the dictionary need to be updated.
static void Main(string[] args)
{
// presumably you will have a separate collection of all your Customer objects somewhere
List<Customer> customers = new List<Customer>();
Customer customer1 = new Customer { Id = 1, FullName = "Jo Bloogs" };
Customer customer2 = new Customer { Id = 2, FullName = "Rob Smith" };
Customer customer3 = new Customer { Id = 3, FullName = "Rob Zombie" };
customers.Add(customer1);
customers.Add(customer2);
customers.Add(customer3);
Dictionary<Customer, int> CustomerOrderDictionary = new Dictionary<Customer, int>();
CustomerOrderDictionary.Add(customer1, 3);
CustomerOrderDictionary.Add(customer2, 5);
// let's just say that we're going to update the value for any customers whose name starts with "Rob"
// use the separate list of Customer objects for the iteration,
// because you would not be allowed to modify the dictionary if you iterate over the dictionary directly
foreach (var customer in customers.Where(c => c.FullName.StartsWith("Rob")))
{
// the dictionary may or may not contain an entry for every Customer in the list, so use TryGetValue
int value;
if (CustomerOrderDictionary.TryGetValue(customer, out value))
// if an entry is found for this customer, then increment the value of that entry by 1
CustomerOrderDictionary[customer] = value + 1;
else
// if there is no entry in the dictionary for this Customer, let's add one just for the heck of it
CustomerOrderDictionary.Add(customer, 1);
}
}
If this is not the case and the only source of Customer objects that you have available is the dictionary itself, then you'll need to perform some kind of cloning/copying of those objects out to a separate list/array prior to iterating over the dictionary for modification. See Jon Skeet's answer for this case; he suggests using a Where filter on the dictionary's Keys property and uses the ToList method to create a separate List<Customer> instance for the purpose of iteration.

Filtering list objects from another list

I have the following class in my C# .NET 3.5 win forms app:
class Field {
string objectName;
string objectType;
string fieldName;
string fieldValue;
}
and a List fieldList that is a datasource for a checkedlistbox. This listbox shows all the distinct objectNames from my fieldList collection.
I want to create another checkedlistbox that contains fieldNames, but only shows fieldnames that have an associated checked objectName in the first list box.
So my question is how can I query the DataSource of the original list of objectNames to return the distinct set of fieldNames that are associated with a selected objectName?
That is not very easy to read so I will give an example:
Field1 {
objectName = 'objA'
FieldName = 'FieldA'
}
Field2 {
objectName = 'objA'
FieldName = 'FieldB'
}
Field3 {
objectName = 'objB'
FieldName = 'FieldA'
}
Field4 {
objectName = 'objC'
FieldName = 'FieldC'
}
So suppose in my checkbox I select objectNames objA and objB. Then my returned fields would be 'FieldA' and 'FieldB'.
How can I achieve this using LINQ or filtering my generic list of Fields? Can I utilise the 'select' or 'where' methods that are available to a list?
First, read the object names into an array or list; I'll fake that part. Then it should be something like:
string[] objectNames = { "objA", "objC" };
var hashSet = new HashSet<string>(objectNames);
var qry = (from row in data
where hashSet.Contains(row.objectName)
select row.fieldName).Distinct().ToList();
(edit)
To get the selected names (the bit I faked) you could try (untested):
var selectedNames = namesCheckedListBox.CheckedItems.Cast<Field>()
.Select(field => field.objectName);
var hashSet = new HashSet<string>(selectedNames);
(note no need to use Distinct() in the above, since HashSet<T> does that anyway)
var selectedNames = ... // List of selected names
var selectedFields = (from f in fieldList
where selectedNames.Contains(f.objectName)
select f.FieldName).Distinct().ToList();

Categories

Resources