2

from..select linq クエリを使用して XML ファイルからデータを読み取っていますが、データが膨大です。私が読み取ることができるデータの最大セットは 1100 行で、各行は数百文字で構成されています。これにより、電話がハングし、アプリケーションがクラッシュします。エミュレーターでは、ロードにかなりの時間がかかりますが、正常に動作します。

今私が欲しいのは、クエリにデータ要素を含めたいいくつかの条件に基づいています。例として、私が今持っているものは...

 var dataSet = from r in something.Elements("chapter")
               select new dictChapter{
                   Name = r.Attribute("Name").Value,
                   Desc1 = r.Attribute("Description1").Value,
                   Desc2 = r.Attribute("Description2").Value,
                   Desc3 = r.Attribute("Description3").Value
               };
ListBox.DataContext = dataSet;

しかし、設定に基づいて説明を選択したいと思います。次のようなものが欲しい(うまくいかないことはわかっているが、やりたいことを説明したい)

 var dataSet = from r in something.Elements("chapter")
               select new dictChapter{
                   Name = r.Attribute("Name").Value,
                   if (ShowDesc1 == true)
                       Desc1 = r.Attribute("Description1").Value,

                   if (ShowDesc2 == true)
                       Desc2 = r.Attribute("Description2").Value,

                   if (ShowDesc3 == true)
                       Desc3 = r.Attribute("Description3").Value
               };
ListBox.DataContext = dataSet;
  1. Cシャープでこれを達成するにはどうすればよいですか?
  2. 私の問題に対するより良い解決策はありますか?

どうもありがとう

4

2 に答える 2

2

条件演算子「?:を使用する

var dataSet = from r in something.Elements("chapter")
               select new dictChapter{
                   Name = r.Attribute("Name").Value,
                   Desc1 = ShowDesc1 ? r.Attribute("Description1").Value : String.Empty,
                   Desc2 = ShowDesc2 ? r.Attribute("Description2").Value : String.Empty,
                   Desc3 = ShowDesc3 ? r.Attribute("Description3").Value : String.Empty,

               };
于 2012-04-07T13:03:01.733 に答える
2

次のようなものを試すことができます:

var dataSet = from r in something.Elements("chapter")
               select new dictChapter{
                   Name = r.Attribute("Name").Value,
                   Desc1 = (ShowDesc1 ? r.Attribute("Description1").Value : null),
                   Desc2 = (ShowDesc2 ? r.Attribute("Description2").Value : null),
                   Desc3 = (ShowDesc3 ? r.Attribute("Description3").Value : null),
               };

すべての Desc1-3 プロパティは常にすべての要素に設定されますが、ShowDesc1-3 が false に従っている場合は null に設定されるため、これは正確には望みどおりではありません。

于 2012-04-07T13:04:40.937 に答える