[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
SolvedPrivate

A linq query against two objects with same property names

Posted on 2014-02-06
11
Medium Priority
?
39 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 64

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 64

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 64

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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 600 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 1400 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

649 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