SolvedPrivate

A linq query against two objects with same property names

Posted on 2014-02-06
11
35 Views
Last Modified: 2016-02-10
I have two generic lists of objects. Each object type contains a Name, Url and Percent property.

Do I have any real need to create an abstract class (or interface) which will contain those three properties?

I need to join the two lists, sort them, and return those three properties from the Top 4 objects returned.

Can I do all this without creating an abstract class or interface containing those three properties?
0
Comment
Question by:newbieweb
  • 4
  • 4
  • 3
11 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39840268
Hi newbieweb;

Nothing special needed. Just do a normal Join on a common property in the tow list. Something like in the pseudocode below

var q = from l1 in MyList1 
        join l2 in MyList2 on l1.joinOnProperty equals l2.joinOnProperty 
        select new 
        { 
        	Property1 = l1.Property1, 
        	Property2 = l1.Property2,
        	Property3 = l2.Property3
        }; 

Open in new window

0
 

Author Comment

by:newbieweb
ID: 39840349
Could you re-write that query using fluent API method calls? I have a hard time getting my head about the format you used.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39840895
What do you mean by "join"? Are you concatenating both lists into one list, or are you actually performing a join like you would in a SQL query?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39841028
Hi newbieweb;
 
The same pseudocode using method syntax.
var resultSet = Mylist1.Join(MyList2,                     // Inner Collection
                             l1 => l1.joinOnProperty,     // Outter Key Selector
                             l2 => l2.joinOnProperty,     // Inner Key Selector
                             (l1, l2) => new              // Result Selector
                             {
                                 Property1 = l1.Property1,
                                 Property2 = l1.Property2,
                                 Property3 = l2.Property3	
                             });

Open in new window

0
 

Author Comment

by:newbieweb
ID: 39841756
Sorry, I was not looking to join. I should have looked more closely at your first post. I just need to understand if the objects contained in the list must share a common ancestor if I need to access the Name, Url and Percent.

I planned to use Add() for the lists. I guess the answer is they must share a common ancestry. True?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39841788
Can you rephrase your requirement? Also, example data would be helpful!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39841811
Is one or more properties that relate the tow list.
0
 

Author Comment

by:newbieweb
ID: 39841827
not relate (like an ID), but both object types have the same properties. Name, Url and Percent. I need to do a single linq expression where I combine the two lists into one query.

Then take Top(4)

 public class ProgramCategoryItemModel
    {
        public string Name { get; set; }
             
        public string Url { get; set; }

        public int Points { get; set; }

        public bool Completed { get; set; }
    }

    public class ProgramCategoryModel
    {
        public string Name { get; set; }

        public string Url { get; set; }

        public int Points { get; set; }

       public bool Completed { get; set; }
}

I expect I will create a single interface for these and can combine the lists and query quite easily. My architect said the interface was an extra file, and suspect he knows the linq to combine the lists and query as one object type. DO you suggest creating an interface they will both enforce?
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 150 total points
ID: 39841851
In each of the two list, List1 and List2, they both have three properties?

You state that they both have the properties Name, Url and Percent. Are those properties spelled the same way in both list?

Is what you want to create one list with all the objects from List1 and List2?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 350 total points
ID: 39841860
It sounds like you are asking this:

var list1 = new List<ProgramCategoryItemModel>(); // assume it has items
var list2 = new List<ProgramCategoryModel>(); // assume it has items

var projectedList1 = list1.Select(item => new
                                          {
                                              Name = item.Name,
                                              Url = item.Url,
                                              Points = item.Points,
                                              Completed = item.Completed,
                                          });
                                          
var projectedList2 = list2.Select(item => new
                                          {
                                              Name = item.Name,
                                              Url = item.Url,
                                              Points = item.Points,
                                              Completed = item.Completed,
                                          });
                                          
var query = projectedList1.Concat(projectedList2)
                          .OrderBy(item => item.PropertyYouWantToOrderBy)
                          .Take(4);

Open in new window


The reason for the "projectedList*" is because when you Select a new anonymous object, the compiler is creating a new, hidden type for you. You cannot instance this new type directly, but LINQ can, and when the compiler sees two anonymous object usages that have the same property definitions, it will try to reuse them. So each projection creates instances of this new type where the properties have the data that was retrieved from the items in the original list. The final query is then working with the exact same types, so it can execute the Concat without issue.
0
 

Author Closing Comment

by:newbieweb
ID: 39852007
Thanks!
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question