2

これは私の古いコードです:

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);
    }
}

これは完璧に機能しましたが、時間がかかりました。

今、私はそれをすべて結合で機能させたいと思っています。これは私がこれまでに持っているものです:

(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();

これは私がこれまでに得たものですが、何も返していません。私は何が間違っているのですか?

4

1 に答える 1

3

DefaultIfEmptyステートメントに括弧がないことに気づきました。また、問題はnullデータへの結合の処理方法にあると思います。私はあなたが間違いなくwhere j1.Any()電話を必要としていると思います、そしてあなたはまた必要かもしれません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(); 

アップデート:

// 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();     

データを使用してモックアップ:http://ideone.com/XEqf4

于 2012-04-20T14:22:21.733 に答える