I have about 20000 rows data in Table in db.
I want to get 3 rows data in this table , but I don't want to get same rows.
Example code:
var SampleTable = (from v in db.Article
select new Item
{
Name = v.Name
});
var distinctItems = SampleTable.GroupBy(x => x.Name ).Select(y => y.First());
var threeItem = distinctItems.Take(3).ToList();
If I use var distinctItems = SampleTable.GroupBy(x => x.Name ).Select(y => y.First()); and get 3 rows.
It work, but large data rows will take a large time to do GroupBy.
But if I take 3 rows in SampleTable first, then remove same rows.
If remove rows happen that will cause my rows count not
meet the requirements count.
Have a better way to do?
Related
Can we display the data table rows where grouped column value is not zero.
first, I need to group by sno and display ungrouped data where amount is not zero.
For example:
I have a data table like below:
need output like this
However I get this with the below code
FinalDataTable = table.AsEnumerable()
.GroupBy(r => new { Col1 = r["sno"]})
.Select(g =>{
var row = table.NewRow();
row["sno"] = g.Key.Col1;
row["amount"] = g.Sum(r => r.Field<decimal>("amount"));
return row;
}).CopyToDataTable();
Yes, what you want to do is Group by sno and then use Where to find those records where the Sum of amount is not zero. Then simply use Select Many to unwrap those groups back into rows.
I think this should do it (I've assumed amount is an int)
FinalDataTable = table.AsEnumerable()
.GroupBy(r => r["sno"])
.Where(g => g.Sum(r => (int)r["amount"]) != 0)
.SelectMany(r => r)
.CopyToDataTable();
Here is a live example: https://dotnetfiddle.net/ixi0aW
I have 5 rows in a Datatable.There is a column named,say "duplicate". Based on that column, i want all those rows which want distinct values in column "duplicate".
And also i want rows which have been rejected.
var duplicateRecords = (from rows in outputTable.AsEnumerable() select rows.Field<string>("duplicate") into grp
where grp.Count() > 1
select grp).Distinct();
The above written code return the values in Column "duplicate". However i want the whole row.
Please help me with this.
Try something like the following:
var duplicateRecords = outputTable.AsEnumerable()
.GroupBy(row => row.Field<string>("duplicate"))
.Where(grp => grp.Count() == 1)
.SelectMany(grp => grp)
.Distinct();
This should return only duplicate rows (Not in front of VS so there might be some typos).
Hope it helps!
I want to find out the count of the distinct values of a particular column of a DataTable and store the result in listbox.
Currently, I have the code to extract the distinct values of a column of a DataTable and store it in a ListBox as follows:-
var ids = dt.AsEnumerable()
.Select(s => new
{
EventID = s.Field<Int32>("ID"),
})
.Distinct().ToList();
listBox.DataSource = ids;
where dt is the name of the DataTable
ID is the name of the column
For Eg:-
Table is
Name ID Marks
Ashish 10 200
Ram 100 300
Sur 200 800
Shim 10 899
Kam 100 989
Then the result in the listbox should appear as:-
{ID=10}{Count=2}
{ID=100}{Count=2}
{ID=200}{Count=1}
Currently I am getting output as:-
{ID=10}
{ID=100}
{ID=200}
Please help!!
You can use GroupBy:
var idCounts = dt.AsEnumerable()
.GroupBy(row => row.Field<int>("ID"))
.Select(g => new
{
EventID = g.Key,
Count = g.Count()
})
.ToList();
listBox.DataSource = idCounts;
I have a datatable which holds 3 columns:
Product, Price, Manufacturer
I am trying to read the data into a list of objects, defining which rows are stored by the following:
Store all Products which have the cheapest price and take the
manufacturer from that line.
EG-
Product, Price, Manufacturer
table, 15.00, ikea
table, 12.50, woodpty
chair, 11.00, ikea
chair, 9.00, woodpty
The expected output into the list is two objects with the following properties:
table, 12.50, woodpty
chair, 9.00, woodpty
I have the following code, but I am getting an error-
String does not contain a definition for 'Name' and no extension
method 'Name' accepting a first argument of type 'string' could be
found (are you missing an assembly reference?)
var result = (
from row in dtProductListings.AsEnumerable()
group row by row.Field<string>("Product") into g
let x = new
{
Name = g.Key.Name, //THIS LINE IS CAUSING THE PROBLEM
Price = g.Min(x => x.Field<float>("Price"))
}
where (row.Name == x.Name && row.Price == x.Price)
select new Foo
{
Name = row.Name,
Manufacturer = row.Manufacturer,
Price = row.Price
}
).ToList();
I am still quite new to LINQ and am wondering where I am going wrong? Any help would be greatly appreciated.
Nice try but your attempt has a few issues. Firstly where you have used Name = g.Key.Name should be Name = g.Key, and secondly your Linq expression will fail to compile also because row is no longer in scope after the group by clause.
Linq can be a tricky thing to get your head around at the start, but what you're effectively trying to achieve is to group the rows by the product column and then select the row in each group which has the lowest price.
So to create the product groups:
var rowsGroups = from row in dtProductListings.AsEnumerable()
group row by row.Field<string>("Product") into g
select g.OrderBy(row => row.Price);
For your example, this will produce two groups of IOrderedEnumerable based on the product values, with the items in each group being order by lowest price to highest price.
Group 1: Key = "table"
Row 1: table, 12.50, woodpty
Row 2: table, 15.00, ikea
Group 2: Key = "chair"
Row 1: chair, 9.00, woodpty
Row 2: chair, 11.00, ikea
So now all your have to do to get your result is to select the first item in each group to get the minimum priced item:
var result = (from row in rowGroups
select row.First())
.ToList();
The same query using lambda expression and linq chain methods (which I find easier to write since it focuses you on the inputs and outputs of what your're doing):
var result = dtProductListings.AsEnumerable()
.GroupBy(row => row.Field<string>("Product"))
.Select(x => x.OrderBy(y => y.Price))
.Select(x => x.First())
.ToList();
Simplified further:
var result = dtProductListings.AsEnumerable()
.GroupBy(row => row.Field<string>("Product"))
.Select(x => x.OrderBy(y => y.Price).First())
.ToList();
Try this:
var result = (
from row in dtProductListings.AsEnumerable()
group row by row.Field<string>("Product") into g
select new
{
Name = g.Key,
Price = g.Min(x => x.Field<float>("Price"))
Manufacturer = g.First().Field<string>("Manufacturer")
}
).ToList();
I am having a few problems quering a DataSet.Tables[0] and removing rows that do not meet the critira of a List.
//This is my list
var values = new List<string> {"Test", "Test2"};
// Now I just query the DataSet for anything that doesnt match this list
var query = from x in ds.Tables[0].AsEnumerable()
from b in values
where !x.Field<string>("ColumnName").Contains(b)
select x;
This works and returns the results but it is returning 2 x sets of the same rows (I assume because there is no join).
How can I just get Distinct values of these rows?
It sounds like you probably want:
var query = from x in ds.Tables[0].AsEnumerable()
where !values.Any(b => x.Field<string>("ColumnName").Contains(b))
select x;
In other words, find all the rows where the "ColumnName" field value isn't present in any of the values in values.
Maybe what you are looking for are the Distinct or the Intersect methods instead of ".Contains" ?
You don't have to join with your values list:
var query = from x in ds.Tables[0].AsEnumerable()
where !values.Any(str => x.Field<string>("ColumnName").Contains(str))
select x;