私の質問には 2 つの部分があります。それを行う方法と、それが適切なスタイルであるかどうかです。
TestDataBaseEntities
渡される DBContext アイテムです。myQuery
実行されて読み取られるクエリですStatusMessage
操作が成功したかどうかを UI が報告する関数から渡されます。Records
理解するのに助けが必要です。各レコードを の呼び出しメソッドに戻す方法を理解する必要がありReadMan()
ます。
DataAccess
このクラスでデータを読み取って、クラスに表示する準備ができているため、返される 2 次元の文字列配列が役立つと考えていましたUserInterface
。しかし、そこに問題があります。
を宣言するにはstring[,]
、文字列のサイズを知る必要があります。または、このようにして暗黙的に寸法を指定する必要がありRecords[,] = new string[,] { { FirstField, SecondField }, { FirstField, SecondField } ... }
ますが、読み取られる最初のレコードは伝えるために必要な情報を提供しないため、できませんRecords
その 2 番目のインデックスが想定される大きさ、 、これが機能していれば、クラス[ , ThisIndex]
の 2dを Display にユーザーに渡します。 Records
UserInterface
なぜこの読み取り機能があるのですか? EF関数をUIから分離することになっているからですよね?
public class DataAccess
{
public bool ReadMan(TestDatabaseEntities dbEntities, IQueryable myQuery, out string StatusMessage, out string[,] Records)
{
string ErrorMessage;
bool bSuccessful;
string[] ThisRecord;
bSuccessful = TryDataBaseAction(dbEntities, out ErrorMessage,
() =>
{
foreach (Man m in myQuery)
{
// was thinking ThisRecord could be temporary storage but doesn't seem to work to my benefit.
ThisRecord = new string[] { m.ManID.ToString(), m.Name };
}
});
if (bSuccessful)
StatusMessage = "Records read successfully";
else
StatusMessage = ErrorMessage;
return bSuccessful;
}
public bool TryDataBaseAction(TestDatabaseEntities MyDBEntities, out string ErrorMessage, Action MyDBAction)
{
UserInterface MyUI = new UserInterface();
try
{
MyDBAction();
ErrorMessage = "No Error";
return true;
}
catch (Exception e)
{
ErrorMessage = e.ToString();
return false;
}
}
}
編集:修正
public bool ReadMan(TestDatabaseEntities dbEntities, IQueryable myQuery, out string StatusMessage, out string[,] Records)
{
string ErrorMessage;
bool bSuccessful;
string[,] TheseRecords = null;
// hands an Action() to TryDataBase, as indicated by lambda expression in 3rd arguement.
bSuccessful = TryDataBaseAction(dbEntities, out ErrorMessage,
() =>
{
List<Man> men = myQuery.OfType<Man>().ToList();
TheseRecords = new string[men.Count, 2];
// ERROR BELOW: Operator '<' cannot be applied to operands of type 'int' and 'method group'
for (int i = 0; i < men.Count; i++)
{
TheseRecords[i, 0] = men[i].ManID.ToString();
TheseRecords[i, 1] = men[i].Name;
}
});
Records = TheseRecords;
if (bSuccessful)
StatusMessage = "Records read successfully";
else
StatusMessage = ErrorMessage;
return bSuccessful;
}