I'm trying to bind the data values to a repeater, with bank names and balance of each bank. The balance is calculated as balance = sum(debit) - sum(credit), and i'm trying to get a result like:
-------------------
bank name | amount|
------------------|
a | 1200 |
------------------|
b | 1500 |
------------------|
c | 2400 |
-------------------
for this i used the code:
protected void bank_account()
{
var balance = 0;
using (var context = new sem_dbEntities())
{
var query = (from b in context.banks
join h in context.heads on b.h_id equals h.h_id
where b.bankstatus != 3 && (h.pid == 13 || h.h_id == 9)
select new { b.acc_name, b.h_id }).Take(4);
foreach (var item in query)
{
var debit1 = (from p in context.ledgers where p.h_id == item.h_id select p.debit).Sum();
var credit1 = (from q in context.ledgers where q.h_id == item.h_id select q.credit).Sum();
balance = Convert.ToInt32( debit1 - credit1);
var query1 = (from b in context.banks
join h in context.heads on b.h_id equals h.h_id
where b.bankstatus != 3 && (h.pid == 13 || h.h_id == 9)
select new { b.acc_name, b.h_id, balance }).Take(1);
foreach (var item1 in query1)
{
Repeater1.DataSource = query1.ToList();
Repeater1.DataBind();
}
}
}
}
Related
I have the following code:
var configItems = new List<ConfigurableItem>();
var assemblies = new List<Assembly>();
var pcbas = new List<Pcba>();
configItems.Add(new ConfigurableItem() { CiCode = "TST001", AssemblyId = "ASS001" });
configItems.Add(new ConfigurableItem() { CiCode = "TST002", AssemblyId = "ASS001" });
configItems.Add(new ConfigurableItem() { CiCode = "TST003", AssemblyId = "ASS002" });
configItems.Add(new ConfigurableItem() { CiCode = "TST004", AssemblyId = "ASS003" });
assemblies.Add(new Assembly { AssemblyId = "ASS001", PcbaId = "PCBA001"});
assemblies.Add(new Assembly { AssemblyId = "ASS002", PcbaId = "PCBA002" });
pcbas.Add(new Pcba { PcbaId = "PCBA001", Pcb = "401"});
var query = from c in configItems
join ca in (from a in assemblies join p in pcbas on a.PcbaId equals p.PcbaId into ca from x in ca.DefaultIfEmpty() select a)
on c.AssemblyId equals ca.AssemblyId into cap
from ca in cap.DefaultIfEmpty()
select new {
CiCode = c.CiCode,
AssemblyId = (ca == null ? "-" : ca.AssemblyId),
Pcb = ???
};
How can I make the LINQ query produce the following result set:
TST001 | ASS001 | PCBA001 | 401
TST002 | ASS001 | PCBA001 | 401
TST003 | ASS002 | - | -
TST004 | - | - | -
This may help
from c in configItems
join ca in (from a in assemblies join p in pcbas on a.PcbaId equals p.PcbaId into ca from x in ca.DefaultIfEmpty() select new {a,x})
on c.AssemblyId equals ca.a.AssemblyId into cap
from ca in cap.DefaultIfEmpty()
select new {
CiCode = c.CiCode,
AssemblyId = (ca == null ? "-" : ca.a.AssemblyId),
Pcb = (ca != null && ca.x != null) ?ca.x.Pcb :"-",
}
Here is working example
I have table Billing as below
AccoundID | Group | DateOfBill
1234 | A | 2017-07-12
1234 | B | 2017-07-16
1234 | C | 2017-07-31
1235 | A | 2017-07-31
1236 | B | 2017-07-31
As you see, AccountID 1234 have made 3 transaction on July 2017. So I need a list where the AccountID 1234 must be in Group C because that's is latest date on that transaction.
Here is my code snippet
var LatestAccount = from n in Billing
where (n.Group == "A" || n.Group == "B" || n.Group == "C")
group n by new { n.AccountID, n.Group } into g
select new {
AccountId = g.Key.AccountID,
Group = g.Key.Group ,
DateOfBill = g.Max(t => t.DateOfBill)
};
But the result is wrong. How to do in LINQ?
class Program
{
static void Main(string[] args)
{
List<Billing> Billings = new List<Billing>()
{
new Billing()
{
AccountID = 1234, DateOfBill = new DateTime(2017,07,12), Group = "A"
},
new Billing()
{
AccountID = 1234, DateOfBill = new DateTime(2017,07,16), Group = "B"
},
new Billing()
{
AccountID = 1234, DateOfBill = new DateTime(2017,07,31), Group = "C"
},
new Billing()
{
AccountID = 1235, DateOfBill = new DateTime(2017,07,31), Group = "A"
},
new Billing()
{
AccountID = 1236, DateOfBill = new DateTime(2017,07,31), Group = "B"
}
};
var LatestAccount = from n in Billings
where (n.Group == "A" || n.Group == "B" || n.Group == "C")
group n by new { n.AccountID } into g
select g.Where(d => d.DateOfBill == g.Max(m => m.DateOfBill)).Select(x => new { AccountId = g.Key.AccountID, Group = x.Group, DateOfBill = x.DateOfBill }).FirstOrDefault();
foreach (var item in LatestAccount)
{
Console.WriteLine("AccountID: " + item.AccountId + " Date of Bill: " + item.DateOfBill + " Group: "+ item.Group);
}
Console.ReadLine();
}
}
class Billing
{
public int AccountID { get; set; }
public string Group { get; set; }
public DateTime DateOfBill { get; set; }
}
Is below what you want?
If you show me the output you want, I can modify my answer.
Currently I am developing an application like SharePoint and I am encountering a difficult as followed.
I have a DB table to keep my contents like the following
+----+---------------+----------+---------+----------+--------------------------+
| ID | Content_type | List_ID | COL_ID | ITEM_ID | VALUE |
+----+---------------+----------+---------+----------+--------------------------+
| 1 | "Column" | 1 | 0 | | "ABC" |
| 2 | "Column" | 1 | 1 | | "DEF" |
| 3 | "Item" | 1 | 0 | 1 | "<VALUE OF Column ABC>" |
| 4 | "Item" | 1 | 1 | 1 | "<VALUE OF Column DEF>" |
+----+---------------+----------+---------+----------+--------------------------+
and I would like to display these record on the web using linq and C# like the following....
ITEM_ID |ABC |DEF
------------+---------------------+----------------------
1 |<VALUE OF Column ABC>|<VALUE OF Column DEF>
EDITED:
My questions are:
I would like to use the DB record stated as Column in the content_type field to be the DataColumn of a DataTable.
I would like to map all records in the DB stated as ITEM with the same Item_ID as 1 DataRow of a DataTable. The value field of each DB records will fall onto the column of the DataTable based on the Column ID.
Thanks for the help. I had make it by myself....
First get the records from DB where content_type = "Column" and use
these records to form a DataTable
Get all records from DB where content_typ= "Item" and re-group each item to a List where ITEM_id =
Map the Item.title = column of the DataTable and Items.value = value of the DataTable rows....
public static DataTable PopulateRec(int list_id, string web_app, string host_auth)
{
DataTable dt = new DataTable();
List Column = Get_Column(list_id, web_app, host_auth);
for (int i = 0; i < Column.Count(); i++)
{
DataColumn datacolumn = new DataColumn();
datacolumn.ColumnName = Column[i].ToString();
dt.Columns.Add(datacolumn);
}
List Items = Get_Item(list_id, web_app, host_auth);
if (Items.Count != 0)
{
int ItemCount = Convert.ToInt32((from Itms in Items
select Itms.Item_id).Max());
for (int j = 0; j <= ItemCount; j++)
{
dt.Rows.Add();
List IndvItem = (from Indv in Items
where Indv.Item_id == j
select Indv).ToList();
foreach (var val in IndvItem)
{
dt.Rows[j][val.title] = val.value;
}
IndvItem = null;
}
for (int k = 0; k < dt.Rows.Count; k++)
{
if (dt.Rows[k][0].ToString() == string.Empty)
{
dt.Rows[k].Delete();
}
}
}
Column = null;
Items = null;
return dt;
}
private static List Get_Column(int list_id, string web_app, string host_auth)
{
List content_db = new List();
List columnname = new List();
Config_DB_Context configdb = new Config_DB_Context("dms_config");
content_db = (from c in configdb.site_mapping
where c.host_auth == host_auth
&& c.web_app == web_app
select c.content_db).ToList();
for(int i = 0; i < content_db.Count(); i++)
{
Content_DB_Context contentdb = new Content_DB_Context(content_db[i]);
columnname = (from c in contentdb.content
where c.content_type == "Column"
&& c.list_id == list_id
select c.title).ToList();
}
content_db = null;
return columnname;
}
private static List Get_Item(int list_id, string web_app, string host_auth)
{
List content_db = new List();
List Itm = new List();
Config_DB_Context configdb = new Config_DB_Context("dms_config");
content_db = (from c in configdb.site_mapping
where c.host_auth == host_auth
&& c.web_app == web_app
select c.content_db).ToList();
for (int i = 0; i < content_db.Count(); i++)
{
Content_DB_Context contentdb = new Content_DB_Context(content_db[i]);
Itm = (from c in contentdb.content
where c.content_type == "Item"
&& c.list_id == list_id
select new MyItem
{
col_id = (int)c.column_id,
list_id = (int)c.list_id,
title = c.title,
value = c.value,
Item_id = (int)c.item_id,
hidden = c.hidden
}).ToList();
}
content_db = null;
return Itm;
}
This linq provides me all rooms as a list grouped by rateCode.
var results = (from r in dcCrs.CRS_RateTypePerReservation
where r.Reservation_id_fk == reservation.Reservation_id_pk
&& r.RoomTypeCenter_id_fk != null
&& r.Price != 0
group r.RoomTypesPerCenter.RoomTypes.Code by r.CRS_RateTypes.Name into g
select new { rateCode = g.Key, roomName = g.ToList() });
But now I have to order the results by an Integer in database, named Order:
var results = (from r in dcCrs.CRS_RateTypePerReservation
where r.Reservation_id_fk == reservation.Reservation_id_pk
&& r.RoomTypeCenter_id_fk != null
&& r.Price != 0
orderby r.Order ascending
group r.RoomTypesPerCenter.RoomTypes.Code by r.CRS_RateTypes.Name into g
select new { rateCode = g.Key, roomName = g.ToList() });
This only orders the name of rooms, not both of them.
Data:
Order Rates RoomType
5 PER DBL
30 PER IND
15 BAR IND
10 BAR DBL
20 BAR URB
It should give this result because the first is 5 and 30 (PER) and then 10, 15 and 20 (BAR):
{rateCode = PER, roomName = {DBL, IND} }
{rateCode = BAR, roomName = {DBL, IND, URB} }
But it returns me this:
{rateCode = BAR, roomName = {DBL, IND, URB} }
{rateCode = PER, roomName = {DBL, IND} }
Thanks for any advice.
The order of keys of database GROUP BY query result is undefined.
You need to apply ordering after the grouping, like this
var results =
(from r in dcCrs.CRS_RateTypePerReservation
where r.Reservation_id_fk == reservation.Reservation_id_pk
&& r.RoomTypeCenter_id_fk != null
&& r.Price != 0
group r by r.CRS_RateTypes.Name into g
orderby g.Min(r => r.Order)
select new
{
rateCode = g.Key,
roomName = (from r in g orderby r.Order select r.RoomTypesPerCenter.RoomTypes.Code).ToList()
});
I've been trying to find a way of using multiple conditions with Sql, at the moment I am using Entity Framework. There is a very good chance that this is the only way to achieve what I want but I wondered if anyone knew a more efficient method.
Essentially I am using criteria to find the next row in the database which should be picked. For example lets use the 2 following conditions:
1) Field1 = A;
2) Field2 = B;
So in the following table:
| RowId | Field1 | Field2 |
| 0001 | A | B |
| 0002 | B | B |
| 0003 | C | C |
| 0004 | A | C |
I need to pick each row individually in the following order:
0001 - Both Condtions Satisfied,
0004 - Condition 1 Satisfied,
0002 - Condition 2 Satisfied,
0003 - No conditions satisfied
At the moment I am doing the following
public TestObj GetNextObj()
{
using (TestDb testDb = new TestDb())
{
TestObj testObj = (from o in testDb.TestTable
where o.Field1 == A && o.Field2 == B
select o).FirstOrDefault();
if (testObj != null)
return testObj;
testObj = (from o in testDb.TestTable
where o.Field1 == A
select o).FirstOrDefault();
if (testObj != null)
return testObj;
testObj = (from o in testDb.TestTable
where o.Field2 == B
select o).FirstOrDefault();
if (testObj != null)
return testObj;
testObj = (from o in testDb.TestTable
select o).FirstOrDefault();
return testObj;
}
}
This works okay, however I want to allow the conditions to be defined in a table and I am worried that when the number of conditions increases that this process will begin taking a very long time.
Is there another way to do what I am attempting here??
Thanks.
EDIT:::::
Now using the following code to select items from a table in order as defined by another table::
public static SortTest GetRow()
{
using (TestDb testDb = new TestDb())
{
SortParam[] sortParams = (from sp in testDb.SortParams
orderby sp.Priority ascending
select sp).ToArray();
if (sortParams.Length == 0)
{
SortTest sortTest = (from st in testDb.SortTests
orderby st.RowId ascending
select st).FirstOrDefault();
Console.WriteLine("Short route");
return sortTest;
}
Console.WriteLine("Long route");
StringBuilder sqlQueryBuilder = new StringBuilder();
sqlQueryBuilder.Append("SELECT * FROM [Proto].[dbo].[SortTests] ORDER BY \n");
foreach (SortParam sortParam in sortParams)
{
sqlQueryBuilder.Append("CASE WHEN " + sortParam.FieldName + " LIKE '%" + sortParam.FieldValue + "%' THEN 1 ELSE 2 END,\n");
}
sqlQueryBuilder.Append("\nRowId"); //By default use row Id
DbSqlQuery<SortTest> dbSqlQuery = testDb.SortTests.SqlQuery(sqlQueryBuilder.ToString());
return dbSqlQuery.FirstOrDefault();
}
}
I may have to alter thigns to prevent Sql Injection, but this works for now.
THANKS!
There's a simple way of doing it in a single query in SQL:
select *
from o
order by case Field1 when 'A' then 1 else 2 end,
case Field2 when 'B' then 1 else 2 end,
RowId