2017-02-09 5 views
1

私は(簡体字)次のクエリからSQL Serverの呼び出しを削減しようとしています:それはどんな違いをした場合ロードし、必要なデータのみのLINQ

var timequery = from t in TimeRecords 
       select t; 

var reducedResults = timequery.Select(delegate(TimeRecord t){ 

    ExpandoObject result = new ExpandoObject(); 
    IDictionary<string,object> record = (IDictionary<string,object>)result; 

    record["DisplayName"] = t.User.DisplayName; //this loads ENTIRE user record 

    //Include Dynamic columns 
    foreach (var expenseTypeDescription in usedExpenseTypes) //the usedExpenseTypes are the types in the results 
    {  
     //also loading entire expense and expense type records 
     record[expenseType] = t.Expenses.Where(e => e.ExpenseType.Description = expenseTypeDescription).Sum(e => e.Amount); 
    } 

    return (dynamic)result; 
}).ToList(); 

私はコピーが1であるreducedResults.OrderBy(t => t.Copies).Skip(page).Take(pageSize)を行いますダイナミック列の経費タイプのうち、後でコードの中に表示されます。だから、私はページングする前にすべての結果をメモリに保存したくありません。

私は生成されたsqlを見て、ユーザーのdisplaynameだけを取得するのではなく、表示名を取得するためにUserレコードを完全に埋め込んでいます。私は表示名を取得するために匿名型をキャストしようとしましたが、その後delegate(TimeRecord t)を使用することはできません。デリゲートを使用しないでも問題はありませんが、費用の種類から動的列をどうやって行うのか分かりません。

要約すると、ユーザーレコード全体をロードできません。

編集:あなたは全体のユーザエンティティを必要といけない場合 簡体字型が

class TimeRecord 
{ 
    User User; 
    EntitySet<Expense> Expenses; 
    //other stuff 
} 

class User 
{ 
    string DisplayName; 
    //other stuff 
} 

class Expense 
{ 
    decimal Amount; 
    ExpenseType ExpenseType; 
    //other stuff 
} 

class ExpenseType 
{ 
    string Description; 
    //other stuff 
} 

答えて

1

あり、それはキャストいないことに

public class UserInfo 
{ 
    public string DisplayName { get; set; } 
    //include anything else you may need. 
} 

var timequery = from t in TimeRecords 
       select new UserInfo 
       { 
       DisplayName = t.User.DisplayName 
       }; 
+0

が必要なフィールドだけが含まれているタイプとキャストを作成しますそれはまったく別のコンセプトです。私はそれを突出と呼ぶだろう。 – DavidG

+0

これはどのように動的費用の列を取得するのですか?それとも動的コードを呼び出してそれをビルドしますか? – SumGuy

+0

@SumGuy no、これはdisplaynameだけを取得し、後でそれからクエリしている他のデータへのアクセスを失います。あなたはテーブル構造やクラス宣言を投稿できますか? –

関連する問題