this is code we have for retrieving a header left joined to its lines (one query), instead of returning two result sets (one for head, one for lines) for getting related data from a database, we always get ONE head, and however many lines it has.
Could you please help me understand why if the Distinct() at the bottom is removed it would return HEAD duplicates corresponding to the number of lines that were retrieved.
Is this because for every row in the reader, we project a HEAD even if it's the same? so if the HEAD has 40 lines, we are projecting the same HEAD 40 times? then a DISTINCT will eliminate 39 and return just one?
Would just doing .FirstOrDefault() without Distinct() be equivalent in this scenario as Distinct() because at the end of the day, it is projecting the same HEAD object?
public static IEnumerable<T> Select<T>(this IDataReader dr, Func<T> selector)
{
if (dr == null)
throw new ArgumentException(nameof(dr));
while (dr.Read())
yield return selector();
}
public void Test()
{
DTOHead head = null;
Dictionary<string, DTOHead> entryDictionary = new Dictionary<string, DTOHead>();
using (DbDataReader reader = cmd.ExecuteReader())
{
var head = reader.Select(dr =>
{
DTOHead entry = null;
if (!entryDictionary.TryGetValue((string)dr["Key"], out entry))
{
DTOHead dtoHead = new DTOHead();
dtoHead.Key = (string)dr["Key"]
dtoHead.Description = (string)dr["DESCRIPTION"];
dtoHead.Lines = new List<DTOLine>();
entry = dtoHead;
entryDictionary.Add(entry.Key, entry);
}
if (dr["LINE_NO"] != DBNull.Value)//skip, there are no lines for this one
{
DTOLine dtoLine = new DTOLine();
dtoLine.LineNo = (string)dr["LINE_NO"];
dtoLine.Qty = (string)dr["QTY"];
entry.Lines.Add(dtoLine);
}
return entry;
}).Distinct();
}
}
Is this because for every row in the reader, we project a HEAD even if it's the same? so if the HEAD has 40 lines, we are projecting the same HEAD 40 times? then a DISTINCT will eliminate 39 and return just one?
Yes, it is this exactly. Your implementation of Select will project one DTOHead for each row in reader. Assuming that you only have one unique "Key" in your result set, it will always be the same DTOHead reference... the one that you create then add to entryDictionary. The call to Distinct then removes all the duplicates and leaves you with an IEnumerable<DTOHead> with one item.
Would just doing .FirstOrDefault() without Distinct() be equivalent in this scenario as Distinct() because at the end of the day, it is projecting the same HEAD object?
Since you indicate that your result set will only contain ONE Head then yeah... you can drop the call to Distinct and just use FirstOrDefault, assuming that you don't want an IEnumerable<DTOHead> and only want an instance of DTOHead.
If that's the case though, you don't need entryDictionary. You can just read the first row from reader, then project the remaining rows into an IEnumerable<DTOLine> with your Select method.
public void Test()
{
DTOHead head = null;
using (DbDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
// Deal with the first row by creating the DTOHead.
head = new DTOHead();
head.Key = (string)reader["Key"];
head.Description = (string)reader["DESCRIPTION"];
head.Lines = new List<DTOLine>();
if (reader["LINE_NO"] != DBNull.Value)//skip, there are no lines for this one
{
// Deal with the first row by creating the first DTOLine.
DTOLine line = new DTOLine();
line.LineNo = (string)reader["LINE_NO"];
line.Qty = (string)reader["QTY"];
head.Lines.Add(dtoLine);
// Project the remaining rows into lines.
head.Lines.AddRange(reader.Select(dr =>
{
DTOLine dtoLine = new DTOLine();
dtoLine.LineNo = (string)dr["LINE_NO"];
dtoLine.Qty = (string)dr["QTY"];
return dtoLine;
});
}
}
}
}
Related
I'm working on creating a filter for a collection of employees. In order to do this I initially fetch a raw collection of all employees. I clone this list so I can iterate over the original list but remove items from the second list.
For each filter I have, I build a collection of employee ids that pass the filter. Having gone through all filters I then attempt to remove everything that isn't contained in any of these lists from the cloned list.
However for some reason, whenever I attempt to do this using .RemoveAll(), all records seemed to be removed and I can't figure out why.
Here is a stripped down version of the method I'm using, with only 1 filter applied:
public List<int> GetFilteredEmployeeIds(int? brandId)
{
List<int> employeeIds = GetFilteredEmployeeIdsBySearchTerm();
List<int> filteredEmployeeIds = employeeIds.Clone();
// Now filter the results based on which checkboxes are ticked
foreach (var employeeId in employeeIds)
{
// 3rd party API used to get values - please ignore for this example
Member m = new Member(employeeId);
if (m.IsInGroup("Employees"))
{
int memberBrandId = Convert.ToInt32(m.getProperty("brandID").Value);
// Filter by brand
List<int> filteredEmployeeIdsByBrand = new List<int>();
if (brandId != null)
{
if (brandId == memberBrandId)
filteredEmployeeIdsByBrand.Add(m.Id);
var setToRemove = new HashSet<int>(filteredEmployeeIdsByBrand);
filteredEmployeeIds.RemoveAll(x => !setToRemove.Contains(x));
}
}
}
return filteredEmployeeIds;
}
As you can see, I'm basically attempting to remove all records from the cloned record set, wherever the id doesn't match in the second collection. However for some reason every record seems to be getting removed.
Anybody know why?
P.S: Just to clarify, I have put in logging to check the values throughout the process and there are records appearing in the second list, however for whatever reason they're not getting matched in the RemoveAll()
Thanks
Ok only minutes after posting this I realised what I did wrong: The scoping is incorrect. What it should've been was like so:
public List<int> GetFilteredEmployeeIds(int? brandId)
{
List<int> employeeIds = GetFilteredEmployeeIdsBySearchTerm();
List<int> filteredEmployeeIds = employeeIds.Clone();
List<int> filteredEmployeeIdsByBrand = new List<int>();
// Now filter the results based on which checkboxes are ticked
foreach (var employeeId in employeeIds)
{
Member m = new Member(employeeId);
if (m.IsInGroup("Employees"))
{
int memberBrandId = Convert.ToInt32(m.getProperty("brandID").Value);
// Filter by brand
if (brandId != null)
{
if (brandId == memberBrandId)
filteredEmployeeIdsByBrand.Add(m.Id);
}
}
}
var setToRemove = new HashSet<int>(filteredEmployeeIdsByBrand);
filteredEmployeeIds.RemoveAll(x => !setToRemove.Contains(x));
return filteredEmployeeIds;
}
Essentially the removal of entries needed to be done outside the loop of the employee ids :-)
I know that you said your example was stripped down, so maybe this wouldn't suit, but could you do something like the following:
public List<int> GetFilteredEmployeeIds(int? brandId)
{
List<int> employeeIds = GetFilteredEmployeeIdsBySearchTerm();
return employeeIds.Where(e => MemberIsEmployeeWithBrand(e, brandId)).ToList();
}
private bool MemberIsEmployeeWithBrand(int employeeId, int? brandId)
{
Member m = new Member(employeeId);
if (!m.IsInGroup("Employees"))
{
return false;
}
int memberBrandId = Convert.ToInt32(m.getProperty("brandID").Value);
return brandId == memberBrandId;
}
I've just done that off the top of my head, not tested, but if all you need to do is filter the employee ids, then maybe you don't need to clone the original list, just use the Where function to do the filtering on it directly??
Please someone let me know if i've done something blindingly stupid!!
I am creating two lists of objects. One "new" list, one "old list".
I want to take the value of one property from an object on the new list and set the property on the old list on the matching object the the new value.
//Original Solution
foreach (RyderQuestion quest in myList)
{
//compare it to every question in ryder questions
foreach (RyderQuestion oldQuestion in _ryderQuestions)
{
//if the question ids match, they are the same question, and should have the right selected option
//selecting the option sets the checkbox of the MultipleChoideQuestionControl
if (oldQuestion.QuestionID == quest.QuestionID)
{
oldQuestion.SelectedOption = quest.SelectedOption;
}
}
}
I am trying to convert it to LINQ to make it more effecient using joins, but how do i update the value directly?
var x = from quest in myList
join oldquest in _ryderQuestions
on new { quest.QuestionID, quest.ShowOn, quest.QuestionOrder }
equals new { oldquest.QuestionID, oldquest.ShowOn, oldquest.QuestionOrder }
select oldquest.SelectedOption = quest.SelectedOption;
This query returns the values to the x list, but I want to actually update the object in the old list instead.
Linq is for querying, not updating. You can join the two lists to line up the object to update, but you'll still have to loop to make the changes:
var query = from quest in myList
join oldquest in _ryderQuestions
on new { quest.QuestionID, quest.ShowOn, quest.QuestionOrder }
equals new { oldquest.QuestionID, oldquest.ShowOn, oldquest.QuestionOrder }
select new {oldquest, quest};
foreach(var item in query}
item.oldquest.SelectedOption = item.quest.SelectedOption
For example:
var x = from quest in myList
join oldquest in _ryderQuestions
on new { quest.QuestionID, quest.ShowOn, quest.QuestionOrder }
equals new { oldquest.QuestionID, oldquest.ShowOn, oldquest.QuestionOrder }
select new {quest , oldquest};
foreach(var item in x)
{
item.quest.SelectedOption = item.oldquest.SelectedOption;
}
You mean this?
Okay so I have to print out a linked list from the order I put it in. Each node refers to a ticket object, and the ticket object has its own print function that I can call upon. I can remove the start of the list and refer it to the next ticket, but have it coded so that it prints out the newest to oldest. I believe the problem lies in my code that allows me to add a ticket to the list:
private class TicketNode
{ //basic node
public TicketNode next;
public Ticket data;
public TicketNode(Ticket tic)
{
data = tic;
}
}
public void PrintAll()
{//Prints all tickets
TicketNode cur = first;
while (cur != null)
{
cur.data.PrintDescription();
cur = cur.next;
}
}
public void AddTicket(Ticket t)
{
TicketNode ticNode; //creates a new node
if (first == null) //for kick-starting the list
first = new TicketNode(t);
else
{
ticNode = new TicketNode(t); //initializes node
ticNode.next = first;
first = ticNode; //first.next is the ticket that was ticNode
}
}
ex: I put in the tickets with strings "Low", "Another Low", and "Final Low" and when I want to print it out I expect:
Low
Another Low
Final Low
Instead I get:
Final Low
Another Low
Low
If I were to remove to oldest ("Low") I should see something like this next time print:
Another Low
Final Low
Any ideas on a how to reorient the list?
The simplest solution would be to insert new items at the end of the list. To do that in O(1), you need to keep a pointer last to the last item in the list. When you insert a new item, you use that pointer to quickly get the last item, append the new item and update the pointer.
With that modification, you can iterate from first via next and actually get the items in their insert order.
While Adding element into linked list, you have find end of the list and there. Following code might be useful for you
AddTicket method is sholud be like this
void AddTicket(Ticket t)
{
TicketNode ticNode; //creates a new node
if (first == null) //for kick-starting the list
first = new TicketNode(t);
else
{
ticNodeNew = new TicketNode(t);
TicketNode ticNode; = first;
while(ticNode.next != null)
{
ticNode = ticNode.next;
}
ticNode.next = ticNodeNew;
}
}
}
In your linked list, the an item references the next oldest, etc. The most recent is first in the list, and the oldest items is at the end of the list. That is why when you run through the list in PrintAll() you get items youngest to oldest.
You need to print your list out in reverse order and an easy way of doing that is to use a Stack.
public void PrintAll()
{
var stack = new Stack<TicketNode>();
TicketNode cur = first;
while (cur != null)
{
stack.Push(cur);
cur = cur.next;
}
while (stack.Count > 0)
stack.Pop().data.PrintDescription();
}
MH09's solution which stores the list in oldest to youngest order is also valid. MH09's solution traverses the entire list on AddTicket(), my solution traverses the list in PrintAll(). You might want to choose which solution is better suited to you on the basis of performance. However in both cases the traversal is O(n).
I have a flat file with an unfortunately dynamic column structure. There is a value that is in a hierarchy of values, and each tier in the hierarchy gets its own column. For example, my flat file might resemble this:
StatisticID|FileId|Tier0ObjectId|Tier1ObjectId|Tier2ObjectId|Tier3ObjectId|Status
1234|7890|abcd|efgh|ijkl|mnop|Pending
...
The same feed the next day may resemble this:
StatisticID|FileId|Tier0ObjectId|Tier1ObjectId|Tier2ObjectId|Status
1234|7890|abcd|efgh|ijkl|Complete
...
The thing is, I don't care much about all the tiers; I only care about the id of the last (bottom) tier, and all the other row data that is not a part of the tier columns. I need normalize the feed to something resembling this to inject into a relational database:
StatisticID|FileId|ObjectId|Status
1234|7890|ijkl|Complete
...
What would be an efficient, easy-to-read mechanism for determining the last tier object id, and organizing the data as described? Every attempt I've made feels kludgy to me.
Some things I've done:
I have tried to examine the column names for regular expression patterns, identify the columns that are tiered, order them by name descending, and select the first record... but I lose the ordinal column number this way, so that didn't look good.
I have placed the columns I want into an IDictionary<string, int> object to reference, but again reliably collecting the ordinal of the dynamic columns is an issue, and it seems this would be rather non-performant.
I ran into a simular problem a few years ago. I used a Dictionary to map the columns, it was not pretty, but it worked.
First make a Dictionary:
private Dictionary<int, int> GetColumnDictionary(string headerLine)
{
Dictionary<int, int> columnDictionary = new Dictionary<int, int>();
List<string> columnNames = headerLine.Split('|').ToList();
string maxTierObjectColumnName = GetMaxTierObjectColumnName(columnNames);
for (int index = 0; index < columnNames.Count; index++)
{
if (columnNames[index] == "StatisticID")
{
columnDictionary.Add(0, index);
}
if (columnNames[index] == "FileId")
{
columnDictionary.Add(1, index);
}
if (columnNames[index] == maxTierObjectColumnName)
{
columnDictionary.Add(2, index);
}
if (columnNames[index] == "Status")
{
columnDictionary.Add(3, index);
}
}
return columnDictionary;
}
private string GetMaxTierObjectColumnName(List<string> columnNames)
{
// Edit this function if Tier ObjectId is greater then 9
var maxTierObjectColumnName = columnNames.Where(c => c.Contains("Tier") && c.Contains("Object")).OrderBy(c => c).Last();
return maxTierObjectColumnName;
}
And after that it's simply running thru the file:
private List<DataObject> ParseFile(string fileName)
{
StreamReader streamReader = new StreamReader(fileName);
string headerLine = streamReader.ReadLine();
Dictionary<int, int> columnDictionary = this.GetColumnDictionary(headerLine);
string line;
List<DataObject> dataObjects = new List<DataObject>();
while ((line = streamReader.ReadLine()) != null)
{
var lineValues = line.Split('|');
string statId = lineValues[columnDictionary[0]];
dataObjects.Add(
new DataObject()
{
StatisticId = lineValues[columnDictionary[0]],
FileId = lineValues[columnDictionary[1]],
ObjectId = lineValues[columnDictionary[2]],
Status = lineValues[columnDictionary[3]]
}
);
}
return dataObjects;
}
I hope this helps (even a little bit).
Personally I would not try to reformat your file. I think the easiest approach would be to parse each row from the front and the back. For example:
itemArray = getMyItems();
statisticId = itemArray[0];
fileId = itemArray[1];
//and so on for the rest of your pre-tier columns
//Then get the second to last column which will be the last tier
lastTierId = itemArray[itemArray.length -1];
Since you know the last tier will always be second from the end you can just start at the end and work your way forwards. This seems like it would be much easier than trying to reformat the datafile.
If you really want to create a new file, you could use this approach to get the data you want to write out.
I don't know C# syntax, but something along these lines:
split line in parts with | as separator
get parts [0], [1], [length - 2] and [length - 1]
pass the parts to the database handling code
Im having a table called transaktions where i inserted customers billings and customer number. My primary key is an int id that has identity specifikation. My question is how do i select all rows that contains a specific customer_nr and returns all the results?
Right now im doing:
public string getCustomerTransaktions(string CustNr)
{
using (var cmd = new SqlCommand("select billing_name from [transaktions] where customer_nr = #customer_nr", Connect()))
{
cmd.Parameters.AddWithValue("#customer_nr", custNr);
using (var er = cmd.ExecuteReader())
{
if (er.Read())
{
return (string)er["billing_name"];
}
}
}
return "UNKNOWN";
}
This will only print the first row that matches the customer nr, there are atleast 20 rows left. Anyone have any suggestion that will help me?
Best Regards
Agree both with #Niklas and #Skurmedel, you need to both use a loop for processing multiple records and collate them together before returning them as a result.
e.g.
public List<string> getCustomerTransaktions(string CustNr)
{
List<string> names = new List<string>();
using (var cmd = new SqlCommand("select billing_name from [transaktions] where customer_nr = #customer_nr", Connect()))
{
cmd.Parameters.AddWithValue("#customer_nr", custNr);
using (var er = cmd.ExecuteReader())
{
while(er.Read())
{
names.Add((string)er["billing_name"]);
}
}
}
return names;
}
Since you do return ... and your method specification pretty much limits the results to one particular value, you can never return more than one billing name. It will return a name as soon as a row has been read, or your default if no rows were returned.
You should put the values in a list or likewise and return that.
try to change the if to a while
while (er.Read())
I guesss a simple solution would be to return the whole ResultSet instead of tring to work around with string. Then iterate through its all items from your calling method