Getting a value from datatable [duplicate] - c#

This question already has answers here:
Filling data at a particular location in a Data Table in C#
(3 answers)
Closed 8 years ago.
I have a Datatable like this.Now how can i pick the value from zones based on weight and zone.I am doing this in c#

Using LINQ Query : You need to use the AsEnumerable() extension for DataTable
var results = from myRow in myDataTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1
select myRow;
AsEnumerable() returns IEnumerable. If you need to convert IEnumerable to a DataTable, use the CopyToDataTable()
Check Here

If you want to pick cells/rows with criteria. you need to a select clauase in your datatable. Go Here to see some examples

Related

Finding duplicates among two datatables [duplicate]

This question already has answers here:
How to perform Dynamic Join using LINQ
(1 answer)
Linq Join with Dynamic Expression
(1 answer)
dynamic join in linq 0 c#
(2 answers)
Closed 5 years ago.
I stuck to find duplicates from 2 datatables where the condition is dynamic.
Let's say TABLEA and TABLEB has same schema
and comparison columns could be anything.
Columns could be more than one.
how can i build a dynamic condition to remove duplicate from TABLEA.
here i tried this query
e.g. i have column name with comma seperated
dynamiccolumnA, dynamiccolumnB, dynamiccolumnC
var matched = from table1 in dt1.AsEnumerable()
join table2 in dt2.AsEnumerable() on
table1.Field<object>(dynamiccolumn) equals table2.Field<object>(dynamiccolumn)
where table1.Field<object>(dynamiccolumn) != table2.Field<object>(dynamiccolumn)
select table1;
" where table1.Field(dynamiccolumn) != table2.Field(dynamiccolumn)" this statment could be for more than one column.
Could anybody gives me some pointer regarding this.
Thanks in advance
Looking at your problem, you need a query which can be customized without having the source code recompiled. I would suggest you to use expression trees. They are tough to understand and can bring complexity but its well worth the price.
Take a look at this article
Microsoft Article on Expression Trees

accessing dataset tables [duplicate]

This question already has answers here:
How to get the identity of an inserted row?
(15 answers)
Closed 7 years ago.
I wonder what is the right approach for getting tables from dataset.
should i return the data within my procedure with select statement?
this is how i do it ,it works but i am not sure this is the right way.
SELECT SCOPE_IDENTITY()
and this is how i access my table:
ds.Tables[0].Rows[0][0]).ToString()
You might want to try using DataRow and looping though the set
foreach(DataRow row in ds.Tables[0])
{
string someData = row["keyname"].ToString();
}
I'm unsure of your question in the first place. But MSDN has a lot of ducumentation, and you can read up on more data structures that can help with DataTables and how to access them. And even query the DataTable with linq like this
var results = from myRow in myDataTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1
select myRow;

Running a Distinct on a record [duplicate]

This question already has answers here:
Distinct in Linq based on only one field of the table
(10 answers)
Closed 7 years ago.
In LINQ if the result that I have is the a list of whole records from the database and I want to do a Distinct on one column of this record, How do I do that?
You could try something as simple as:
var distincts = records.Select(x=>x.ColumnName).Distinct();

LINQ 2 SQL Using Contains [duplicate]

This question already has answers here:
Using contains() in LINQ to SQL
(7 answers)
Closed 8 years ago.
I am trying to convert the following SQL statement to a Link2SQL statement.
SELECT * FROM Global.CustomData
WHERE CustomDataSource LIKE '%Plugin%'
I have converted it to this statement
var query =
from item in db.CustomDatas
where item.CustomDataSource.Contains(dataSource)
select item;
And have tried setting dataSource to the following: "Plugin", "%Plugin%", "/Plugin/" and "%/Plugin%/". These I have taken from other examples. Unfortunately, although the TSQL statement does return a value, I cannot get the Linq2Sql statement to return anything. Could someone tell me what I am doing wrong?
You should pass "Plugin", the only thing I can think of is the case sensitivity. Try something like this:
where item.CustomDataSource.ToLower().Contains(dataSource.ToLower())

"Between" in Linq C# [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
LINQ Between Operator
Dear All,
Hi,
I need to write this query in LINQ C#. can anyone help me?
Select *
From Mytable
where MyText BETWEEN 'john' AND 'Pear'
I believe this query should work:
var results = yourTable.Where(x => x.Text.CompareTo("john") > 0 &&
x.Text.CompareTo("Pear") < 0);
This assumes that you want to compare the text in each row of the table, and not some pre-dfined string.
Here is how you can do it with ObjectQuery
MytableSet.Where("it.Name between #start and #end", new ObjectParameter("start", "john"), new ObjectParameter("end", "Pear"))
EDIT:
Forget to mention that this statement is specific to Entity Framework not LINQ2SQL.

Categories