SolvedPrivate

A linq query against two objects with same property names

Posted on 2014-02-06
11
33 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
DevOps Toolchain Recommendations

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

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

821 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