I got a database with members, each member has a list of sports they do.
now I want to loop through a listbox and add every selected item to my database.
This is my database :
And this is my code :
foreach (var item in sportCheckedListBox.CheckedIndices)
{
int sportindex = Convert.ToInt32(item.ToString()) + 1;
var queryResult = from sp in context.Sports
where sp.sportnr == sportindex
select sp;
foreach (var sport in queryResult)
{
myMember.Sports.Add(sport);
}
}
This looks kinda 'shady', how could I do this better ?
One thing I'd do for sure is move the query out of the loop. Queries should never exist in loops for performance and maintainability reasons. LINQ knows how to translate a (new int[] { 0, 1, 2, ... }).Contains(column) construct into a WHERE column IN (0, 1, 2, ...) statement, so let's use that:
// Get all checked items together
var lookupIndices = sportCheckedListBox.CheckedIndices.Select(i => Convert.ToInt32(item.ToString()) + 1);
// Find all matching sport numbers
var queryResult = from sp in context.Sports
where lookupIndices.Contains(sp.sportnr)
select sp;
// Now loop over the results
foreach (var sport in queryResult)
{
myMember.Sports.Add(sport);
}
// save changes
I think you can just do AddRange:
myMember.Sports.AddRange(queryResult);
myMember.Sports.SaveChanges()
You may need to covert queryResult to an IEnumerable type if it's not already though.
There is nothing fundamentally wrong with your approach, but you can achieve it more concisely with Linq.
Instead of your foreach loop, if you always want to assign a new list you could use
myMember.Sports = queryResult.ToList();
If you want to instead concatenate results to an existing list, you could use
myMember.Sports = myMember.Sports.Concat(queryResult.ToList());
If you wanted to do the same as above, but not have any duplicates (as defined by the object you are adding), instead
myMember.Sports = myMember.Sports.Union(queryResult.ToList());
Related
I have the following logic:
loop through a list of ids, get the associated entity, and for that entity, loop through another list of ids and get another entity. Code is below:
foreach (var docId in docIds)
{
var doc = new EntityManager<Document>().GetById(docId);
foreach (var tradeId in tradeIds)
{
var trade = new EntityManager<Trade>().GetById(tradeId);
if (doc.Trade.TradeId != trade.TradeId)
{
Document newDoc = new Document(doc, trade, 0);
new EntityManager<Document>().Add(newDoc);
}
}
}
my question is mainly about sql performance. Obviously there will be a bunch of selects happening, as well as some adds. Is this a bad way to go about doing something like this?
Should I, instead, use a session and get a list of all entities that match the list of ids (with 1 select statement) and then loop after?
It depends only on my expirience. But you can test it yourselve.
If Trade entity isn't very big and count of entities wouldnt be over 1000 - reading all entities and loop after will be much preferable.
If count is more 1k - its better to call stored procedure with joining temp table, containing your ids.
I am fairly new to C#
I am trying to retrieve some information from an external data source and store it in array, once it is in an array I wish to sort it by time.
I know how to do this for just one column in a row, however the information I require has multiple columns.
For example:
foreach (Appointment Appoint in fapts)
{
// Store Appoint.Subject, Appoint.Start, Appoint.Organiser.Name.ToString(), Appoint.Location in an array
}
// Sort my array by Appoint.Start
foreach ( item in myNewArray )
{
//print out Appoint.Subject - Appoint.Start, Appoint.Organiser.Name.ToString() and Appoint.location
}
Many thanks for your help.
EDIT:
I have multiple data sources which pull in this:
foreach (Appointment Appoint in fapts)
{
// Store Appoint.Subject, Appoint.Start, Appoint.Organiser.Name.ToString(), Appoint.Location in an array
}
Hence the need to sort the items in a new array, I know this isn't very efficent but there is no way of getting the information I need in any other way.
You can sort a list using the LINQ sorting operators OrderBy and ThenBy, as shown below.
using System.Linq;
and then...
var appointments = new List<Appointment>();
var sortedAppointments = list.OrderBy(l => l.Subject).ThenBy(l => l.Name).ToList();
This will create a new list of appointments, sorted by subject and then by name.
It's unclear what your final aim is but:
Use a generic List instead of an array:
See this SO question for more information as to why using a List is prefered.
List<Appointment> appointments = new List<Appointment>();
foreach (Appointment Appoint in fapts)
{
appointments.Add(Appoint);
}
foreach (var item in appointments)
{
Console.WriteLine(item.Subject);
Console.WriteLine(item.Foo);
// Here you could override ToString() on Appointment to print eveything in one Console.WriteLine
}
If the aim of your code is to order by time, try the following:
var sortedAppointments = fapts.OrderBy(a => a.Start); // assuming Start is a DateTime property of `Appointment`.
Consider a Dictionary Object instead of an array if the data is conceptually one row multiple columns.
foreach(KeyValuePair<string, string> entry in MyDic)
{
// do something with entry.Value or entry.Key
}
You already have a list of objects in fpts, sort that list itself:
fpts.OrderBy(x => x.Subject).ThenBy(x => x.Location).ToList();
LINQ is your friend here.
fapts appears to already be a collection so you could just operate on it.
var myNewArray = fapts.OrderBy(Appoint => Appoint.Start).ToArray()
I've used the ToArray() call to force immediate evaluation and means that myNewArray is already sorted so that if you use it more than once you don't have to re-evaluate the sort.
Alternatively if you are only using this once you can just as easily miss the ToArray() portion out and then execution of the sort will be deferred until you try and enumerate through myNewArray.
This solution puts the source objects into the array, but if you are just wanting to store the specific fields you mention then you will need to use a select. You have two choices for the array item type, you can either use an anonymous class which provides difficulties if you are returning this array from a function or define a class.
For anonymous:
var myNewArray = fapts.OrderBy(Appoint => Appoint.Start)
.Select(Appoint => new {
Start = Appoint.Start,
Organiser = Appoint.Organiser.Name.ToString(),
Location = Appoint.Location
}).ToArray();
For named class assuming class is MyClass:
var myNewArray = fapts.OrderBy(Appoint => Appoint.Start)
.Select(Appoint => new MyClass {
Start = Appoint.Start,
Organiser = Appoint.Organiser.Name.ToString(),
Location = Appoint.Location
}).ToArray();
You have a wide range of options. The 2 most common are:
1) Create a class, then define an array or list of that class, and populate that
2) Create a structure that matches the data format and create an array or list of that
Of course, you could put the data into an XML format or dataset, but that's probably more work than you need.
public List<foo> appointments = new List<foo>();
public struct foo
{
public string subject ;
public DateTime start ;
public string name ;
public string location ;
}
public void foo1()
{
// parse the file
while (!File.eof())
{
// Read the next line...
var myRecord = new foo() ;
myRecord.subject = data.subject ;
myRecord.start = data.Start ;
myRecord.name = data.Name ;
//...
appointments.Add(myRecord);
}
}
Enjoy
(Since I can't comment and reply to the comment - it wasn't clear if he had a class, etc. or was just showing us what he wanted to do. I assumed it was just for demonstration purposes since there wasn't any info as to how the data was being read. If he could already put it into a class, than the first answer applied anyway. I just tossed the last 2 in there because they were options for getting the data first.)
I don't understand why my variable selected doesn't contain the content of all the TempX variable. For example, in my case, the variable TempX containt one SuperObject but as soon as I reach the first intersect, it's lost and my View always show an empty list...
By the way, the blablabla.ToList() are real and complicated linq query. I put this to make it clearer.
Thanks and here is the code:
public ActionResult Search(string q)
{
ViewBag.q = q;
String[] strQueries = q.Split(' ');
IEnumerable<SuperObject> selected = new List<SuperObject>();
foreach (string str in strQueries)
{
//Query 1
IEnumerable<SuperObject> Temp1 = blablabla.ToList();
//Query 2
IEnumerable<SuperObject> Temp2 = blablabla2.ToList();
//Query 3
IEnumerable<SuperObject> Temp3 = blablabla3.ToList();
//Query 4
IEnumerable<SuperObject> Temp4 = blablabla4.ToList();
selected = selected.Intersect(Temp1);
selected = selected.Intersect(Temp2);
selected = selected.Intersect(Temp3);
selected = selected.Intersect(Temp4);
}
return View("Search", selected);
}
You probably want to use Union instead of Intersect. Here's the difference, I think it's self explanatory:
You are intersecting an empty list with Temp 1-4. This isn't going to yield any results.
Edit: To elaborate, an intersect gets all elements common to two collections. Since the first collection you are intersecting is empty the result is always going tobe empty. So the answer really depends on what you are trying to do. Are you trying to find only elements common to all 4 lists? if so do what BurundukXP said:
selected = Temp1.Intersect(Temp2);
selected = selected.Intersect(Temp3);
selected = selected.Intersect(Temp4);
Are you trying to get a unique list of all the elements in each list? Do something like this:
selected.AddRange(Temp1);
selected.AddRange(Temp2);
selected.AddRange(Temp3);
selected.AddRange(Temp4);
selected.Distinct();
Can anyone suggest how this can be improved?
public IEnumerable<Person> FindPersons(string queryTerms)
{
if (queryTerms == null)
return new List<Person>();
var queryTermsList = queryTerms.Split(' ').ToList();
var first = queryTermsList.First();
queryTermsList.Remove(first);
var people = FindPerson(first);
foreach (var queryTerm in queryTermsList)
{
people = people.Intersect(FindPerson(queryTerm));
}
return people;
}
Basically what it does is searches for people that contain EVERY queryTerm within the queryTermList.
Because results have to contain ALL terms I used Intersect.
Because I was using intersect I had to do an initial search for the first query term outside the foreach loop so the intersect within the loop would have something to intersect with. Otherwise you’d obviously always get empty results.
This meant I then needed to remove the first query term from the list before entering the foreach loop.
Ok, so this works. It just seems there must be a more elegant way of writing this.
Any suggestions?
You can just start with the entire collection and intersect all of the terms with that:
var people = AllPeople;
foreach (var queryTerm in queryTermsList)
{
people = people.Intersect(FindPerson(queryTerm));
}
I have created a method to remove duplicates froma a DataView. I have not option to change the SQl query , so my only option is to modify the existing data retrieved from the Database in the DataView.
DataView data
Id, Name, Date
1, Paul, 12-05-2011
2, Mark, 12-05-2011
1, Paul, 12-05-2011
2, Mark, 12-05-2011
My method is:
private static void RemoveDuplicates(DataView source, string keyColumn)
{
DataRow[] dataRows = new DataRow[source.Table.Rows.Count];
source.Table.Rows.CopyTo(dataRows, 0);
var uniquePrimaryKeys = new List<Guid>(duplicateTable.Rows.Count);
foreach (DataRow row in duplicateTable.Rows)
{
if (uniquePrimaryKeys.Contains((Guid)row[keyColumn]))
source.Table.Rows.Remove(row);
else
uniquePrimaryKeys.Add((Guid)row[keyColumn]);
}
}
I wonder if there is a better method to achieve the same result but faster.
Actually, ADO.NET added a(n apparently not well known) feature that allows you to create a new table containing the distinct entries from an existing table. Here's how it works:
.....
.....
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/ed9c6a6a-a93e-4bf5-a892-d8471b84aa3b/
Could you use a linq group as an alternative? I couldn't say how much quicker it would be but I dare say it will be well optimised.
var result = from x in source.Table.AsEnumerable()
group x by new { id = x.Field<int>("ID"), Name = x.Field<string>("Name"), Date = x.Field<DateTime>("Date") }
into groupedResults
select groupedResults.Key;