SolvedPrivate

A linq query against two objects with same property names

Posted on 2014-02-06
11
36 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 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