If / Else in Inline Synatx declare a class object with C # properties

I have a requirement to dynamically add property values ​​based on column values. Current I am creating an object as shown below:

while (rd.Read()) { list.Add(new DeviceModelInfo() { ID = rd.GetGuid("ID"), Manufacturer = new Manufacturer() { ID = rd.GetGuid("ManufacturerID"), Name = rd.GetString("ManufacturerName"), ManufacturerType (ManufacturerEnum)rd.GetInt32("ManufecturerType") }, Model = rd.GetString("Model"), IMEI = rd.GetString("IMEI") }); } 

But I need to check if the IMEI column is available or not in the DataReader , but when I use if else, then its syntax error I want to add IMEI if it is available in the DataReader , how can I do it?

  if (rd.GetName(i).Equals("IMEI", StringComparison.InvariantCultureIgnoreCase)) IMEI = rd.GetString("IMEI") 
+5
source share
4 answers

I assume i already a variable where there is a column number which will be IMEI if one is available. If this is not the case, see End of answer.

You cannot use the if in an object initializer. What you can do is split it into a separate statement:

 while (rd.Read()) { var device = new DeviceModlInfo { ID = rd.GetGuid("ID"), Manufacturer = new Manufacturer() { ID = rd.GetGuid("ManufacturerID"), Name = rd.GetString("ManufacturerName"), ManufacturerType (ManufacturerEnum)rd.GetInt32("ManufecturerType") }, Model = rd.GetString("Model"); }; if (rd.GetName(i).Equals("IMEI", StringComparison.InvariantCultureIgnoreCase)) { device.IMEI = rd.GetString("IMEI"); } list.Add(device); } 

Alternatively, you can use the conditional ?: operator to assign a single value or IMEI value based on a condition:

 while (rd.Read()) { list.Add(new DeviceModelInfo { ID = rd.GetGuid("ID"), Manufacturer = new Manufacturer() { ID = rd.GetGuid("ManufacturerID"), Name = rd.GetString("ManufacturerName"), ManufacturerType (ManufacturerEnum)rd.GetInt32("ManufecturerType") }, Model = rd.GetString("Model"), IMEI = rd.GetName(i).Equals("IMEI", StringComparison.InvariantCultureIgnoreCase)) ? rd.GetString("IMEI") : null; }); 

}


If you really don't know which column will be IMEI, I would probably write a separate method to check if a column is present or not, only once. Then you write (outside the loop):

 int? imeiColumn = GetColumn(dr, "IMEI"); 

Then in the initializer of the object write:

 IMEI = imeiColumn != null ? dr.GetString(imeiColumn.Value) : null 

Where GetColumn might look something like this:

 static int? GetColumn(DbDataReader reader, string name) { for (int i = 0; i < reader.VisibleFieldCount; i++) { if (reader.GetName(i).Equals(name, StringComparison.InvariantCultureIgnoreCase)) { return i; } } return null; } 
+3
source

You can use this extension method:

 public static bool ColumnExists(this IDataRecord dr, string ColumnName) { for (int i=0; i < dr.FieldCount; i++) { if (dr.GetName(i).Equals(ColumnName, StringComparison.InvariantCultureIgnoreCase)) return true; } return false; } 

Using:

 while (rd.Read()) { if(rd.ColumnExists("IMEI")) // DO YOUR WORK } 

Another way to use LINQ:

Only work with .NET 3.5 and higher

  while (rd.Read()) { if(Enumerable.Range(0, rd.FieldCount).Any(c => rd.GetName(c) == "IMEI")) // DO YOUR WORK } 
+1
source

You can try using the null-coalescing statement to check if a column has a value.

 IMEI = rd.GetString("IMEI") ?? "" 
0
source

You can use the conditional operator ?: To check if an IMEI column exists or not. If a Column exists, then get the value otherwise, set the IMEI value as No IMEI (whatever)

So change

 IMEI = rd.GetString("IMEI") //Inside list.Add(new DeviceModelInfo()..) 

to

 IMEI = rd.GetName(i).Equals("IMEI", StringComparison.InvariantCultureIgnoreCase) ? rd.GetString("IMEI"):"No IMEI" 
0
source

Source: https://habr.com/ru/post/1276222/


All Articles