LINQ FindAll returning same item twice - c#

I have a table in my database with three columns and two rows:
The columns are: Id, PersonId and ListName
The values in the first row are: 1, 1, 'ListOne'
The values in the second row are: 2, 1, 'ListTwo'
When I try to select these items I'm using LINQ:
var values = dbContext.Table.FindAll().ToList();
The object values has 2 items, just like the table itself. The problem is that it's selecting the first row twice and ignoring the second row, instead of selecting each row just once.
What can I do to retrieve these values from my table correctly?
I've already tried different LINQ methods and they all returned the same thing.

Problem solved! The error was in my model class. I was overriding the Id with the PersonId value.

Related

Compare 2 large DATA tables(70k Records)

How do I compare 2 large DATA tables in C#? The DataTable.Select method takes forever.
I need to compare each record’s field value with the other table. The source and target field data type might be different, e.g. Table1’s field1 data type is INT and Table2’s field1 datatype is VARCHAR.
You need to profile application to find what exactly is slow: iterating through columns and comparing values or finding matching records (rows) that needs to be compared.
As for record, solution could be to convert a table to dictionary. That works if your tables have unique column, then you can convert them to dictionary, where key is unique column value for record and value is whole row. Then iterate first DataTable, get unique column value and get row from 2nd Datatable, but from Dictionary.
If issue is in comparison between 2 rows, then it is better to show the code to see, maybe there are extra comparisons or casts. It's hard to tell without code.

Entity returns same registry for all rows

I'm trying to get all rows from a table using the following:
using (var context = new dbcontext()){
return context.table.ToList();
}
my problem is: this command returns 1232 registry (count on table) with the same data in all rows, even if I exec a .Distinct();
PS: I have 1232 different registries
Does this entity have an ID column? If so, then all rows will always be different, since the ID's are unique.
You should select the relevant columns (excluding the ID column at least) in an anonymous, and THEN do the .Distinct().

sum of values in each column in a database table

I have a table like this:
Table name is List.
I have to get sum of each column. I cannot give this query:
SELECT SUM(jayesh,murali,jins) from List;
because the columns are added dynamically. Column names are given as user input. Is there any other code to do this..?
If you want the sum in each column:
SELECT SUM(jayesh), SUM(murali), SUM(jns) from List;
Not saying that any of this is or is not a good idea because no context was provided, but...
Sum of all columns combined for all records (one sum for entire table):
SELECT SUM(jayesh+murali+jns)
FROM List
Sum of all columns for each record (one sum for each row):
(This option requires having an id column to group by so that you can determine how to group the rows)
SELECT
ID,
SUM(jayesh+murali+jns)
FROM List
GROUP BY ID
Sum of each column separately for all records (one sum for each column):
SELECT
SUM(jayesh),
SUM(murali),
SUM(jns)
FROM List
I would also recommend reconsidering your design in adding dynamic columns based on user input. This is generally not a good idea, certainly not for this case. Read more here: http://www.sommarskog.se/dynamic_sql.html

How to loop data from a table in sql server and using the returned data to query another table. Please see

This is what I am trying to achieve :
I have a table in a database in which there is a column named "item_code".
I need to query this table, returning ONE row at a time. Now, I need to use the value of the column "item_code" for the one row which has been returned to query ANOTHER table where I'll use it to fetch a bunch of row(s). How do I do this?
I tried using a datareader object in a while loop, fetch one row at a time and then query the other table inside this loop to fetch the rows required but I couldn't figure out how to put this data in a gridview (use datatable? if yes, how?) in such a way that the previous rows in the gridview don't get erased after each iteration of the while loop.
The only way I know for putting data into a gridview is by using .Fill() but obviously, Fill method wouldn't do in this case as it would wipe out the previous entries in the gridview.
Please help.
Your solution will work, but you are correct, Fill() will erase the contents of the table. Instead, use Merge()
var myMainTable = new DataTable();
foreach(var itemId in itemIds)
{
var currentTable = new DataTable();
// submit new query
myAdapter.Fill(currentTable)
myMainTable.Merge(currentTable);
}
I need to query this table, returning ONE row at a time. Now, I need to use the value of the column "item_code" for the one row which has been returned to query ANOTHER table where I'll use it to fetch a bunch of row(s). How do I do this?
You could use a single SQL query that joins the two tables on "item_code" and retrieves the results from the second table.
You can make it in SQL level in one step, like this:
SELECT Table2.*
FROM Table2
INNER JOIN Table1 ON Table1.item_code = Table2.item_code
ORDER BY Table2.item_code
And of course if you need to make a smaller list, you can wirte the WHERE too.

Remove duplicated from ONLY FIRST column in DataTable in C#

I have a DataTable containing 10 rows and 2 columns which is created from a mdx query, I want to convert this DataTable into html table but before that I want remove duplicated from ONLY FIRST column. How can I do that?
Use LINQ and query it yourself:
myTable.Tables[0].Rows.GroupBy(x => x["Column1Name"]);
Your rows should now be grouped by a distinct Column1Name value, and you can then access it from there.

Categories

Resources