how to combine two data source into one? - c#

i have records in two tables, and 1 object,
i want to retrieve data from both tables into 1 gridview, (both tables have same fields) i can not have joins because i need to show all rows
here is my code:
var query = from all in DB.Movies
where all.IsActive
select new MoviesObject
{
PhotoId = all.PhotoId,
Title = all.Title,
Description = all.ShortDescription
};
var querytwo = from all in DB.movieslisttwo
where all.IsActive
select new MoviesObject
{
PhotoId = all.PhotoId,
Title = all.Title,
Description = all.ShortDescription
;
return query.ToList();

return query.Concat(query2).ToList();
Alternatively, you can call .Union() to skip duplicates.

Related

C# linq expression not pulling the data correctly

I am trying to query two tables and need to pull related records from both the tables. I am using enityframeworkcore 3 One is system versioned table and the other is history table. My resultset is containing data only from history table and not system-versioned table. Could somebody tell me what is wrong with my statement . I am ensured that the personid in the systemversion table matches the history table.
Query
var personNotes = (from pn in _context.PersonNotes
join pnh in _context.PersonNotesHistory on pn.PersonId equals pnh.PersonId
select pn);
return personNotes;
You need to specify the column names you want to return in the result:
var personNotes = (from pn in _context.PersonNotes
join pnh in _context.PersonNotesHistory on pn.PersonId equals pnh.PersonId
select new {
data1 = pn.column1,
data2 = pn.column2,
data3 = pn.column3,
data4 = pnh.column1,
data5 = pnh.column2,
data6 = pnh.column3,
}).ToList();
return personNotes;
Just change the column1, column2, column3 with column names you want to retrieve from the database.
As an alternative to the first answer you may use this syntax:
var personNotes = _context.PersonNotes
.Join(_context.PersonNotesHistory, pn => pn.PersonId, pnh => pnh.PersonId, (pn, pnh) => new
{
pn.Note,
pn.AuthorID,
pn.CreatedBy,
pnh.Note,
pnh.AuthorID,
pnh.CreatedBy
})
.ToList();

Linq join on multpile columns with a where not equals condition and need to return columns from both tables

I am joining tables on two columns and need to find records where the third column does not match.
The linq query I have will return the correct records from table 1, but I am unable to select the date column from the second table.
I have tried grouping results but had trouble with the 'where not equals' condition.
So, I have these records:
Table 1: 4SONS, 112, 09/03/2016
Table 2: 4SONS, 112, 09/26/2016
What I need to return is - 4SONS, 112, 09/03/2016, 09/26/2016
But my query returns only - 4SONS, 112, 09/03/2016 without a way to get that date from table 2. These are the only 3 columns in each table.
var query = from s in schedTable.AsEnumerable()
where s.Field<DateTime?>("AuditDate").HasValue
join c in completeTable.AsEnumerable()
on new { account = s.Field<string>("Account").ToString(), store = s.Field<string>("Store").ToString()}
equals new { account = c.Field<string>("Account").ToString(), store = c.Field<string>("Store").ToString() }
where s.Field<DateTime>("AuditDate").Date != c.Field<DateTime>("AuditDate").Date
select s;
var typeD = query.ToList();
This will do what you are looking for:
var query = from s in schedTable.AsEnumerable()
where s.Field<DateTime?>("AuditDate").HasValue
join c in completeTable.AsEnumerable()
on new { account = s.Field<string>("Account").ToString(), store = s.Field<string>("Store").ToString()}
equals new { account = c.Field<string>("Account").ToString(), store = c.Field<string>("Store").ToString()}
where s.Field<DateTime>("AuditDate").Date != c.Field<DateTime>("AuditDate")
select new {
account = s.Field<string>("Account"),
store = s.Field<string>("Store"),
sched = s.Field<DateTime>("AuditDate"),
comp = c.Field<DateTime>("AuditDate")
};
var typeD = query.ToList();

LINQ query to retrieve from one table and then another

I have 2 tables containing the same fields,
e.g. id,name,invoiceNo..etc
I want to use c# ling to get all the data from both tables
I have the below example for 1 table, how do I add the second table?
return query = from tb1 in dataContext.tbl1
select new customer
{
name= tbl1.name
};
You can just use Concat once you have two sequences of the same type.
return dataContext.tbl1.Select(tb1 => new customer()
{
name = tb1.name,
})
.Concat(dataContext.tbl2
.Select(tb2 => new customer()
{
name = tb2.name,
}));
You could use query syntax for the select calls, but I find method syntax preferable in this particular case.
You can use Union or Concat:
query1 = from tb1 in dataContext.tbl1
select new customer
{
name= tbl1.name
};
query2 = from tb2 in dataContext.tbl2
select new customer
{
name= tbl1.name
};
var resQuery = query1.Union(query2);
It would be similar with Concat. Main difference between Union and Concat is that Union removes duplicities from the result.

fill linq null data c# from previous line

I have bill details in list which contains the heading:
Account Numbers, Dates, Service Number, Charge category, Details, exgst, ingst, Detailsfill
Column :- Account Numbers, Dates, Service Number, Charge category, Details, exgst, ingst
are unique values but Column Detailsfill contains null. I want to allow my C# script to set values for Detailsfill where Detailsfill is null then insert data from previous row otherwise do nothing.
Note that Running total sets to 1 when detailsfill column has any text but continues to increment by 1 from 3 when detailsfill column is empty.
Below is the script I used.
var result_set2 = (from a in result_set1
join b in
(from x in result_set1a select x)
on
new {col1 = a.id, col2 = a.account, col3 = a.date, col4 = a.service, col5 = a.chargecat }
equals
new { col1 = b.id, col2 = b.account, col3 = b.date, col4 = b.service, col5 = b.chargecat } into outer
from tb in outer.DefaultIfEmpty()
where a.running_total1 != 0
where a.running_total1 != 2
where a.chargecat != ""
select new
{
running_total = a.running_total1,
account = a.account,
date = a.date,
service = a.service,
chargecat = a.chargecat,
details = a.details,
exgst = a.exgst,
ingst = a.ingst,
detailsfill = ((tb == null) ? "" : tb.details)
}).ToList();
foreach (var line in result_set2)
{
mobilenet_writerfile.WriteLine(line);
}
It sounds to me like you need a correlated subquery to populate detailsfill. This would be in the form of a select from a context with whatever parameters you determine fulfill your requirements. Example on second post of this thread: http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/18da37b0-b40e-444e-8ec7-7bd343e0c799/
Also you can find other examples here on stackoverflow: Is it possible to create a correlated subquery in LINQ?
More information would be required to write a contextual example but here is basically what it would look like:
Entities context = new Entities();
context.tbl.Where(t=>t.Id == someId)
.Select(t=> new() {
t.Id,
context.tbl2.First(tbl2=>tbl2.Id == t.Id).Value, //This is the subquery
t.whatever});
This is not a normal operation of Linq to hold onto a previous value and then use it later. That's not to say you can't make that work with Linq, but it would be more straightforward to keep that logic outside of the sequence operation. Simply iterate over the results after the query is composed.
var item = results.First();
foreach (var obj in results)
{
if (obj.detailsfill == null)
obj.detailsfill = item.detailsfill;
item = obj;
}
Note: If the first item happens to contain null in detailsfill, you will not pick up a value until the first item in the sequence that does contain a value. To get the first such item up front, simply use
var item = results.First(o => o.detailsfill != null);
// loop below

taking more than one cells into one cell

I am using a linq query to obtain a table of customers with their total money amount for each monetary unit exist in my database(this one is ok.)
when show the result of my query with Microsoft Report Viewer the result is like Table 1 but what i want is Table 2, only the customer name like "A" and a cell with all the monetory unit records > 0.
Is there any way you can suggest?
This is my code which produces Table 1:
var query = from kur in kurToplamlist
join cariBilg in db.TBLP1CARIs
on kur.CariIdGetSet equals cariBilg.ID
select new
{
cariBilg.ID,//customerid
EUROBAKIYE = cariBilg.HESAPADI,
cariBilg.K_FIRMAADI,//other column names
cariBilg.K_YETKILIADI,//other column names
cariBilg.K_FIRMATELEFON,//other column names
cariBilg.K_YETKILITELEFON,//other column names
AUDBAKIYE = cariBilg.B_CEPTELEFON,//other column names
MonetaryUnit = String.Concat(kur.KurToplamMiktarGetSet.ToString(), kur.DovizTuruGetSet.ToString()),//concatenates "100" and "TL/USD etc."
};
What i want is to obtain Table 2 in the image
Thank you in advance.
Table image
var query = from kur in kurToplamlist
where kur.KurToplamMiktarGetSet > 0
join cariBilg in db.TBLP1CARIs
on kur.CariIdGetSet equals cariBilg.ID
select new
{
cariBilg.ID,
EUROBAKIYE = cariBilg.HESAPADI,
cariBilg.K_FIRMAADI,
cariBilg.K_YETKILIADI,
cariBilg.K_FIRMATELEFON,
cariBilg.K_YETKILITELEFON,
AUDBAKIYE = cariBilg.B_CEPTELEFON,
TLBAKIYE = String.Concat(kur.KurToplamMiktarGetSet.ToString(), kur.DovizTuruGetSet.ToString()),
};
var dfg = from qre in query
select qre.TLBAKIYE;
var aq = (from qw in query
select new {
qw.ID,
EUROBAKIYE = qw.EUROBAKIYE,
qw.K_FIRMAADI,
qw.K_YETKILIADI,
qw.K_FIRMATELEFON,
qw.K_YETKILITELEFON,
AUDBAKIYE = qw.AUDBAKIYE,
TLBAKIYE = String.Join(",", (from qre in query
where qre.ID == qw.ID
select qre.TLBAKIYE).Distinct())
}).Distinct();
return aq;
This is my answer.

Categories