score:-1
i think if you explain more precisely what do you want to do in your view we will be able to help you better.
when i see you code, i deduce that your table in your view will display just one record with his appropriate properties/columns. if you want to do that, its ok.
on the other hand, iterate over a table according to the number of columns is a bad idea because your are coupling two abstraction levels. what i mean is that your mvc method should not have knowledge of the number of columns and this is what your are doing with "for(int i = 1; i < 100; i++)". indeed, what happen if there is a table schema update and the number of columns changes ? the same for the name of the columns ? your code will be break !
i think you are taking the problem from the wrong side to me.
score:0
there is no reason to repeatedly take the first element from the same unchanging collection. simple take the first element from the collection, and then work with that retrieved object:
var element = db.datatable.firstordefault();
this already dramatically simplifies your code and improves performance (because you don't need to iterate over the collection for every property):
if(element.d1 != null)
{
//do code
}
// and so on...
it is unclear exactly why you need to ascertain that each field contains a not-null value. you never posted any code that shows how you intend to use these values.
//here i would add the value (0 for null, 1 for data) to an array, which i would handle in the view
generally, it's better to simply ensure that your view can handle nulls, as this means you don't need to worry about your code executing when a certain value is null.
if that is not possible, then it's still easier to simply convert null
values into a default not-null value. this means you still don't have to worry about nulls (after you've converted them) since you won't have any null values anymore.
a simple way to do this is by using the null coalescing operator, e.g.
var firstname = element.d1 ?? string.empty;
this ensures that null
values get replaced by a default value (which you can specify, i just chose an empty string here but you can pick whatever you want).
also note that you don't even need to do this for non-nullable values such as int
, as these will default to 0
automatically.
//here i would add the value to an array
why use an array instead of an object? it's unclear why you are deadset on manually handling every property (and its value) instead of passing the object by itself, which is far and away the better oop way of passing data.
it's contradictory to at the same time want to handle all data manually and also not want to have to handle all that data.
Source: stackoverflow.com
Related Query
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How to dynamically query the database using LINQ and a variable?
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- How to assign LINQ Query to a variable and then use it later in the code
- How can I write the following code more elegantly using LINQ query syntax?
- How to use expressions to build a LINQ query dynamically when using an interface to get the column name?
- LINQ - database query using "into", how do I reference data prior to "into" in the select clause?
- How to query XML with the same element and attribute name using linq
- How to query values associated with the foreign key using linq and display in listview
- How to dynamically group a list and select values using linq query in C#?
- Get data from database using linq query and display it in dynamically generated labels in repeater
- I have a LINQ statement that is partially query and partially method. How can I do this using one or the other?
- How can I simplify (speed up) the selecting query from a database that contains more than 1 million records using LINQ
- Querying the database using EF Code First and Linq
- Get the Value of WPF Listview rows and compare them with database field using Linq query
- How can I filter a dictionary using LINQ and return it to a dictionary from the same type
- Query Microsoft Access MDB Database using LINQ and C#
- Take the first five elements and the last five elements from an array by one query using LINQ
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- How can I code an outer join using LINQ and EF6?
- How to query many-to-many relationship with 'AND' condition using LINQ and Entity Framework
- How to get the value of class properties with Linq query and IEnumerable
- how to convert Database Hierarchical Data to XML using ASP.net 3.5 and LINQ
- Is a full list returned first and then filtered when using linq to sql to filter data from a database or just the filtered list?
- LINQ Query How to select Max value between start and end index and the index of the max value
- Best way to join 3 lists and count how many times an ID is in the list using linq
- How can I get the top three players and their high scores from a collection using linq and lambdas
- How to Join in LINQ using Lambda expressions and get the result in DTO?
- How can we express the following code using query expression?
- How do you write a LINQ query that filters a sub table to a specific time period and sums the results of the sub table?
More Query from same tag
- linq query with list
- Sort a list with where condition in linq c#
- Translation exception difference in EF Core's different Linq queries
- When to use BlockingCollection and when ConcurrentBag instead of List<T>?
- How can I rewrite this Linq to XML query?
- How do I write an exists subquery using multiple columns in LINQ?
- ASP.Net - Mvc5 : LINQ , Saving duplicate record problem
- How to Use Effeciently Where Clause or Select in LINQ Parallel in Large Dataset
- Convert SQL to LINQ query to get max
- How to Convert Byte Array to Image inside in Linq output List
- How to Trim elements before filling dropdownlist using C# LINQ
- Process MusicBrainz Web Service
- Joining 3 tables and using a left outer join with linq in EF Core 3.1.1
- C# convert 1D array to 2D
- Linq insert on primary key field that is autosync
- Linq to XML treating results individually instead of listing
- Linq - Casting IQueryable to IList returns null - WHY?
- Entity Framework related objects using generics
- Using EF to assign the results of a LINQ query to another object
- Getting xml Attribute value in Linq with Lambda
- Why throws exception when using Guid.Parse() method?
- Adding a character at the beginning of a string if it does not exist using LINQ
- How to include 2 navigational properties in EF?
- LINQ to Entities - Get results based on join, conditions and sorting
- Check value in datatable with Linq
- Get Range of integers with linq
- EF query that includes additional column with aggregated data
- Counting With C# Using Linq Or NOT
- Query vs Extension Method - Using GroupBy
- Boolean shortcircuit evaluation in Linq lambda expression