Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7850839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:59:13+00:00 2026-06-02T18:59:13+00:00

This is my old code: var destinctList = HoldingsList.DistinctBy(x => new { x.ClientName, x.ProductName

  • 0

This is my old code:

var destinctList = HoldingsList.DistinctBy(x => new { x.ClientName, x.ProductName }).Select(x => new { ClientName = x.ClientName, ClientProductName = x.ProductName }).ToList();

foreach (var item in destinctList)
{

    var clientAlias = (from ca in someContext.ClientAlias
                       where ca.Name == item.ClientName
                       orderby ca.CreationDate descending
                       select ca).FirstOrDefault();

    if (clientAlias != null)
    {
        destinctList.Where(x => x.ClientName == item.ClientName).ForEach(x => x.ClientID = clientAlias.ClientID);
    }

    ClientProductAlias clientProductAlias = null;

    if (clientAlias != null)
    {
        clientProductAlias = (from ca in someContext.ClientProductAlias
                              where ca.Name == item.ClientProductName
                              && ca.ClientID == clientAlias.ClientID
                              orderby ca.CreationDate descending
                              select ca).FirstOrDefault();
    }

    if (clientProductAlias != null)
    {
        destinctList.Where(x => x.ClientProductName == item.ClientProductName).ForEach(x => x.ClientProductID = clientProductAlias.ClientProductID);
    }
}

This worked perfectly, but it took to long.

Now i want to make it all work with joins. This is what i have so far:

(from list in destinctList
join ca in someContext.ClientAlias on list.ClientName equals ca.Name into list_client_join
from list_client in list_client_join.DefaultIfEmpty
join cpa in someContext.ClientProductAlias on new { ClientID = (long)list.ClientID, Name = list.ClientProductName } equals new { cpa.ClientID, cpa.Name } into j1
from j2 in j1.DefaultIfEmpty
orderby list_client.CreationDate descending
orderby j2.CreationDate descending
select new { ClientID = list_client.ClientID, 
             ClientName = list.ClientName, 
             ClientProductID = j2.ClientProductID, 
             ClientProductName = list.ClientProductName }).ToList();

This is what i got so far, but is not returning anything. What am i doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:59:15+00:00Added an answer on June 2, 2026 at 6:59 pm

    I noticed that your DefaultIfEmpty statements were missing parenthesis and also I think the problem is how it’s handling joins to null data. I think you definitely need the where j1.Any() call and you might also need the where list_client_join.Any().

    (from list in destinctList 
    join ca in someContext.ClientAlias on list.ClientName equals ca.Name into list_client_join 
    //where list_client_join.Any ()
    from list_client in list_client_join.DefaultIfEmpty() 
    join cpa in someContext.ClientProductAlias on new { ClientID = (long)list.ClientID, Name = list.ClientProductName } equals 
       new { cpa.ClientID, cpa.Name } into j1 
    // maybe needs the following:
    where j1.Any ()
    from j2 in j1.DefaultIfEmpty() 
    orderby list_client.CreationDate descending 
    orderby j2.CreationDate descending 
    select new { ClientID = list_client.ClientID,  
                 ClientName = list.ClientName,  
                 ClientProductID = j2.ClientProductID,  
                 ClientProductName = list.ClientProductName }).ToList(); 
    

    Update:

    // alternate query
    (from list in destinctList  
    let ca = someContext.ClientAlias
                .OrderByDescending (cca => cca.CreationDate)
                .FirstOrDefault (cca => cca.Name == list.ClientName)
    let cca = someContext.ClientProductAlias
                .OrderByDescending (ccpa => ccpa.CreationDate)
                .FirstOrDefault(ccpa => int.Equals(ccpa.ClientID,
                               ca == null ? -1 : ca.ClientID) && 
                              string.Equals(ccpa.Name,list.ClientProductName))
    select new
    {
        ClientID = ca != null ? ca.ClientID : -1,
        ClientName = list.ClientName,
        ClientProductID = cca != null ? cca.ClientProductID : -1,
        ClientProductName = list.ClientProductName 
    }
    ).ToList();     
    

    Mock up using data: http://ideone.com/XEqf4

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using this code: var pendingRequest = new Ajax.Request(myUrl, { method: 'post', postBody: soapMsg, contentType:
I had this working in an old code that I didn’t end up using...
We have some code kicking around that uses this old internal Sun package for
So I'm going through old code (2.0) and I came across this: object isReviewingValue
I am dealing with an old code designed for iPhone OS 2.0. In this
This morning we found an old chunk of code that was causing a library
Current I have this code: var imgCount = 36; var container = $('#3D-spin'); var
This is the code without any attempt to add var nn = 99 to
I inherited this old TurboBasic code base, and I am converting it to something
<Contents> <Content Book=ABC> <Item type=New id=1 File=book1.out/> <Item type=Old id=2 File=book1.out/> </Content </Contents> In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.