I am making an application that will get information from the exchange server and put it in a list. the list i want is made by 2 commands and give me 2 lists aswell. what i am looking for is an efficient way to putting these 2 lists together.
List one has the following objects
string Name
string alias
string email
List two has the folowing ones
string alias
decimal itemcount
double size
What would be the best way of making these 2 lists one? I want to display it in one Dataview.
Also if possible please include Examples/References. im still pretty new to this all.
I assume that you want one entry for each user, where the alias field is the key to join in. In that case, the easiest is to use LINQ:
var newList = (from a in list1
join b in list2
on a.alias equals b.alias
select new
{
a.Alias,
a.Name,
a.Email,
b.ItemCount,
b.Size
}).ToList();
It will solve the problem. Unless you have extremely high volumes of data and responsiveness demands it will be efficient enough.
Related
I am having difficulty trying to use LINQ to query a sql database in such a way to group all objects (b) in one table associated with an object (a) in another table into an anonymous type with both (a) and a list of (b)s. Essentially, I have a database with a table of offers, and another table with histories of actions taken related to those offers. What I'd like to be able to do is group them in such a way that I have a list of an anonymous type that contains every offer, and a list of every action taken on that offer, so the signature would be:
List<'a>
where 'a is new { Offer offer, List<OfferHistories> offerHistories}
Here is what I tried initially, which obviously will not work
var query = (from offer in context.Offers
join offerHistory in context.OffersHistories on offer.TransactionId equals offerHistory.TransactionId
group offerHistory by offerHistory.TransactionId into offerHistories
select { offer, offerHistories.ToList() }).ToList();
Normally I wouldn't come to SE with this little information but I have tried many different ways and am at a loss for how to proceed.
Please try to avoid .ToList() calls, only do if really necessary. I have an important question: Do you really need all columns of OffersHistories? Because it is very expensive grouping a full object, try only grouping the necessary columns instead. If you really need all offerHistories for one offer then I'm suggesting to write a sub select (this is also cost more performance):
var query = (from offer in context.Offers
select new { offer, offerHistories = (from offerHistory in context.OffersHistories
where offerHistory.TransactionId == offer.TransactionId
select offerHistory) });
P.s.: it's a good idea to create indexes for foreign key columns, columns that are used in where and group by statements, those are going to make the query faster,
The Title is a little bit complicating.
The Question here is easy:
If got a CheckBoxList. In this List you are allowed to do multiple choice. I put every chosen value from the Checkboxlist into a list because i need it for my where clause. So i have:
List<int> queueIDList = new List<int>();
Short version of my LINQ:
var reports = from t in tickets
where t.queue_id == every value in queueIDList
select t.ticketnumber;
So how do i have to write it when i want every ticketnumber from DB which is the same like in the queueIDList?
For better knowing - in the CheckBoxList u can chose different Queues, at least u have to chose 1 (null is not allowed). I added the ID's of the chosen Queues to a list and now i want to have every ticketnumber from DB where the queueID equals with the values from the queueIDList.
I think the answer is easy but i'm really stuck with my mind.
Thanks for every help!
You can just use Contains:
var reports = from t in tickets
where queueIDList.Contains(t.queue_id)
select t.ticketnumber;
I wanted to know if there is a way using Linq to object to get a list from filtering 2 other lists.
I have two lists of objects A and B, they are related two each other by an atribute(Code:String). B has another atribute, Name:String.
I want to get a list of A objects that meet 2 conditions.
-All of A objects must match their A.Code atribute to any of B.Code atribute in the B List.
-B.Name must be = "yoda";
I tried with this code(and another exampeles)
but it doesent seem to work and i dont know why.
I am just starting with linQ.
List<A> FilteredAList = (from OneA in ListOfA
join OneB in ListOfB
on OneA.Code equals OneB.Code
where OneB.Name == "yoda"
select OneA).ToList<A>();
Thanks in Advance!.
With your requirement, I think we should use Any method, therefore we should write method query not expression query. Of course I don't know the equivalent of Any in expression query (At least it should be short as in the method query, otherwise, it's not good). If any one knows, please leave some comment. Thanks for that.
var FilteredAList = ListOfA.Where(x=>ListOfB.Any(y=>x.Code==y.Code && y.Name=="yoda"))
.ToList<A>();
I parse in a file and save the results to a list in C#. I then parse all results of a DB query to another list. I want to compare the two lists for matches and save the results to another list to write an output file. I thought comparing the two lists was faster and better than running the query against the DB each time for each person since their is about 16k records in the file.
List 1. List fileList contains datatypes of: string personsName
List 2. List DBresults contains datatypes of: string personsName, string address, string phoneNumber
Trying to compare List 1 to List 2 to find matches and then send results to another list to write output.
Their could be multiple results per person in List 2 so I need to find every instance.
Started with a LINQ query like this but I'm no master at LINQ so I need some help.
var matches = fileList.Where(a=>a.personsName == DBResults.where(s=>s,personsName).toList();
I'm also open to other suggestions if you think you may have a faster way. I know IDctionarys and Hashtables are fast look up tables but I have three datatypes so I can't do a keyvaluepair.
If I understand correctly your problem, the solution is the LINQ Join method.
var result = (from a in fileList
join s in DBResults on a.personsName equals s.personsName
select s).ToList();
Or using the extension method syntax:
var result = fileList.Join(DBResults, a => a.personsName, s => s.personsName, (a, s) => s).ToList();
This method is extremely fast and internally uses HashSet to improve performances.
I find it really hard to explain what is going on, I'll try my best.
We need to make a really simple browser with really simple Favorites + History capabilities, and to do that we need to import them both in a DataSet and use them from there.
The knowledge that I have should be enough, but I would really like to do it more efficient en cleaner. In the database I have 3 tables, one for users, one for favorites and one for history, this is linked with FK's etc. I want a query that returns me every Fav + History url that a user has saved. This is what I have now:
SELECT u_id, u_user, h_url, f_url FROM Users, Favorites, HistoryWHERE u_id = h_id AND u_id = f_id AND u_id = 1
This isn't the result I'm looking for, I want to fill 2 comboboxes, one with favorites and one with history that both are from the person that is logged in at that moment.
I know this should work with join but inner and outer both give to little or to many results, and left and right join don't seem to work for me either, but I cant explain why. :p I'm semi-new to joins btw.
It sounds like you actually want two simple queries, one for favorites and one for history. Both queries just need to have a where clause limiting the user for whom the results are returned.
SELECT f.url
FROM Favorites f
WHERE f.userID = 1
SELECT h.url
FROM History h
WHERE h.userID = 1
If you want to populate two comboboxes, I would suggest that you retrieve two separate lists, one for each combobox. Then you can bind each list separately to its combobox.
While DataSets may not be the best choice, I think they will work for you in this situation. Your objective will be to write two SQL statements, one for each list. Then, you put the query results into DataTables which are contained in one DataSet. A DataTable can be boung to a combobox.