I want to list the province and district using lookupedit, but it gives an error, please help
var iller = (from x in db.TBL_ILLER
select new
{
x.ID,
x.SEHIR
}).ToList();
LkpIll.Properties.ValueMember = "ID";
LkpIll.Properties.DisplayMember = "SEHIR";
LkpIll.Properties.DataSource = iller;
I have listed the provinces like this, but I cannot list the counties
Related
cmd = new SqlCommand(" ", con);
da = new SqlDataAdapter(cmd);
DataTable t_eight = new DataTable("t_eight");
da.Fill(t_eight); //databale "t_eight" fill
//linq query
var query3 = (from a in t_eight.AsEnumerable() //t_eigth Databale
group a by new
{ //Group by with condition
IdendityName = a.Field<String>("Idendity Name"),
ContactPersonName = a.Field<String>("Contact Person Name"),
CustomerName = a.Field<String>("Customer Name")
}
into g
select new
{
IdendinyName = g.Key.IdendityName,
ContactPersonName = g.Key.ContactPersonName,
CurtomerName = g.Key.CustomerName,
TotalBook = g.Sum(x => x.Field<Int32>("Total Book Quantity")
+ x.Field<Int32>("Quatan Book Qty")
- x.Field<Int32>("Return Book Quality"))
});
GridView1.DataSource = query3;
GridView1.DataBind();
Datatable contains columns Identity Name, customer Name, customer Contact Name, Total Book Quantity, Return Book Quality, and Quatan Book Qty.
Group by contains three column Identity Name, customer Name, and customer Contact Name
New column in select statement Total Book contains ( total book quant+Quatan Book Qty-Return Book Quality). I want all columns in the gridview but the grid contains Identity Name, customer Name, customer Contact Name, and total book columns
How can I do this?
Dataset table "t_eight"
Gridview missing column Quatan Book and Return Book
Assuming you expect the additional fields to be identical once the rows are grouped, you just need to pick a random row from the group to get the values - the First row should work.
var query3 = (from a in t_eight.AsEnumerable()//t_eigth Databale
group a by new
{//Group by with condition
IdendityName = a.Field<String>("Idendity Name"),
ContactPersonName = a.Field<String>("Contact Person Name"),
CustomerName = a.Field<String>("Customer Name")
}
into g
select new {
g.Key.IdendityName,
g.Key.ContactPersonName,
g.Key.CustomerName,
ReturnBookQuality = g.First().Field<Int32>("Return Book Quality"),
QuatanBookQty = g.First().Field<Int32>("Quatan Book Qty"),
TotalBook = g.Sum(x => x.Field<Int32>("Total Book Quantity")
+ x.Field<Int32>("Quatan Book Qty") - x.Field<Int32>("Return Book Quality"))
}
);
Note: I removed the redundant member names from the anonymous type, I don't see any reason to put them there.
If I understand you correctly, you want to see the totals of three columns besides the calculated TotalBook column. Here's how you can do that:
var query3 = (from a in t_eight.AsEnumerable()
group a by new
{
IdendityName = a.Field<String>("Idendity Name"),
ContactPersonName = a.Field<String>("Contact Person Name"),
CustomerName = a.Field<String>("Customer Name")
}
into g
let tbq = g.Sum(x => x.Field<Int32>("Total Book Quantity"))
let qbq = g.Sum(x => x.Field<Int32>("Quatan Book Qty"))
let rbq = g.Sum(x => x.Field<Int32>("Return Book Quality"))
select new
{
IdendinyName = g.Key.IdendityName,
ContactPersonName = g.Key.ContactPersonName,
CurtomerName = g.Key.CustomerName,
TotalBookQuantity = tbq,
QuatanBookQuantity = qbq,
ReturnBookQuantity = rbq,
TotalBook = tbq + qbq - rbq
});
The three let statements collect the individual sums per grouping, after which you can use the variables tbq etc. multiple times.
I have a text file and to read from and convert each line to and object with Id and someText. I would like to group them so that I have two lists: unique list and duplicate list. the data is very big up to hundred of thousand of lines. Which is the best data structure to use? Please provide some sample code in C#. Thanks a lot!
for example:
original list read from text file:
{(1, someText),(2, someText),(3, someText),(3, someText1),(4, someText)}
unique list:
{(1, someText),(2, someText),(4, someText)}
duplicate list:
{(3, someText),(3, someText1)}
Here's an example with LinQ
Random rnd = new Random();
StreamReader sr = new StreamReader("enterYourPathHere");
string line = "";
int cnt = 0; //This will "generate our ids".
List<KeyValuePair<int,string>> values = new List<KeyValuePair<int, string>>();
while ((line = sr.ReadLine()) != null)
{
//You convert the line to your object (using keyvaluepair for testing)
var obj = new KeyValuePair<int, string>(cnt, line);
values.Add(obj);
//Increment the id on with 50% chances
if (rnd.Next(0,1) >0.5) cnt++;
}
var unique = values.GroupBy(x=>x.Key).Distinct().Select(x=>x).ToList();
var duplicates = values.GroupBy(x => x.Key).Where(x => x.Count() > 1).Select(x => x).ToList();
I have the 2 following tables: Country and Postal
I retrive all the countries in a DropDownAddCountry and i wan't by doing that to display all the postals belonging to the country in another dropdown (DropDownAddPostals).
The country table have a coulmn CountryID and postal also have a coulm CountryID. So i wan't the result to be based on match between CountryID and CountryID (from both tables):
My code look like this now (and it's not correct):
using (DB_Entities tt = new DB_Entities())
{
var sql = from q1 in tt.Country
join q2 in tt.Postal on q1.CountryID equals q2.CountryID
select new { q2.Postal1 };
if(sql != null)
{
DropDownAddPostal= sql.Postal1;
}
}
Cheers
Don't use anonymous types (especially if they are not necessary).
You can set the collection to your DropDownList with the DataSource-Property.
using (var tt = new DB_Entities())
{
var sql =
from q1 in tt.Country
join q2 in tt.Postal on q1.CountryID equals q2.CountryID
select q2.Postal1
DropDownAddPostal.DataSource = sql.ToList();
DropDownAddPostal.DataBind();
}
I Want to find the value from dataset column Id.
here is the dataset
Id Value
1 football
2 Tennis
3 Cricket
If any one is absent in Column then i want to append that particular value in the dataset
I guess that is a DataTable inside a DataSet. First you need to query if the id is in the DataTable:
var dataTable = dataSet.Tables[0]; //For this example I'm just getting the first DataTable of the DataSet, but it could be other.
var id = 1;
var value = "football";
//Any(...) will return true if any record matches the expression. In this case, the expression is if a Id Field of the row is equals to the provided id
var contained = dataTable.AsEnumerable().Any(x =>x.Field<int>("Id") == id);
Then, if it's not there, add a new row:
if(!contained)
{
var row = dataTable.NewRow();
row["Id"] = id;
row["Value"] = value;
dataTable.Rows.Add(row);
}
Hope it helps
First you should use a loop to see if your dataset column 'id' contains the value. If the id is not existing then:
DataRow newrow = ds.Tables[0].NewRow(); //assuming ds is your dataset
newrow["id"] = "your new id value";
newrow["value"] = "your new value";
ds.Tables[0].Rows.Add(newrow);
how group by in Linq with 2 Field ?
(from i in info
group i by i.OrderId into g
select new { orderId = g.Key, infos = g });
not only order by with order Id but with two field like ...
group by i.orderId And i.City
how this will do?
I believe you want something like this:
var result = from i in info
group i by new { OrderId = i.OrderId, City = i.City } into g
select new { OrderId = g.Key, Infos = g };
Creating the key as an anonymous type simply allows LINQ to use the default equality comparers for all the fields of the anonymous type, which should do the job in most situations.
As a follow-up to Noldorin's answer, you can omit the field names on the anonymous type when they match the fields you're setting them to.
Example:
var result = from i in info
group i by new { i.OrderId, i.City } into g
select new { OrderId = g.Key, Infos = g };
Another follow-up to the Noldorin's and Josh Einstein's answers...OrderID will take on the entire key...which in this case is a new object with two properties, OrderID and City. If your final result set needs the OrderID to be the OrderID, then you'll need to do the following:
var result = from i in info
group i by new { i.OrderId, i.City } into g
select new { OrderId = g.Key.OrderId, Infos = g };