SolvedPrivate

A linq query against two objects with same property names

Posted on 2014-02-06
11
32 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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 74

Expert Comment

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

Expert Comment

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

Author Comment

by:newbieweb
Comment Utility
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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 150 total points
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 350 total points
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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 …
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now