I want to fetch record from self reference table parent record contain multiple child record from these multiple record i have to show maximum offer value ,I also want to show child record from another child table too my query like this
var query = (from pd in _context.Product_User_Transactions_Main
join od in _context.Product_User_Transactions_Yarn on pd.TransactionID equals od.TransactionID
into t
where (pd.UserPortalID == UserID && pd.TransactionTypeID == TransactionTypeID)
select new
{
pd.TransactionID,
pd.ProductName,
pd.Quantity,
pd.Price,
pd.TransactionTypeID,
pd.UOMID,
pd.UOM.UOM1,
pd.RefTransactionID,
pd.PaymentTermID,
pd.UserPortalID,
pd.UserSessionID,
pd.TransactionTime,
Offer = (from m in _context.Product_User_Transactions_Main join el in _context.Product_User_Transactions_Main on m.TransactionID equals el.RefTransactionID where el.RefTransactionID != null select el.Price).Max(),
pd.GEN_PaymentTerm.PaymentTermName,
Product_User_Transactions_Yarn = t.Select(p => new { p.YarnBlendID, p.YarnColorID, p.YarnCount, p.YarnDesc, p.YarnPly, p.YarnSourceID, p.YarnTypeID, p.TransactionID }),
}).ToList();
This query returns a wrong result as this query show Max of one record to all I want to show specific offer in front of it price. Where is my mistake? I don't know
Related
I have a C# application where I am using Entity Framework to pull data from a database. This is the code I am executing:
var person = new List<Person>();
using (DevTestEntities db = new DevTestEntities())
{
person = (from p in db.People
join e in db.PersonEmails on p.Id equals e.Id
join t in db.PersonPhones on p.Id equals t.Id
where t.Phone == phoneNumber
select p).ToList();
}
var str = Newtonsoft.Json.JsonConvert.SerializeObject(person);
return str;
When the code runs, it fails on the select. I assume it is failing because there is a table within the database that is not part of the model. And because there is just a generic select, I assume Entity Framework is selecting all columns from all tables and doesn't know what to do with some of the columns.
What I really want to do is to be able to specify the columns that I want to return to the calling function. How do I specify what columns Entity Framework should select?
Thanks for any assistance.
EF will not fail because of tables in the DB that are not in model. It would help if you provided the error. Also, your query will result in selecting all columns from the People table but not the others.
An example answer to your question is this, it selects three columns from different tables and puts them in a new anonymous type:
var onlySomeColumns = (from p in db.People
join e in db.PersonEmails
on p.Id equals e.Id
join t in db.PersonPhones
on p.Id equals t.Id
where t.Phone == phoneNumber
select new {p.Id, e.email, t.phonenumber}).ToList();
I have two tables, from 1st table i want to get all records and from 2nd table i want the max id value of that record. I am using entity framework in asp.net c#.
i tried the below code but it takes only single record from first table i.e tblblogs. and leave all the records, how to get all those records by using this query? plz help me out I'll be very grateful to you. Thanks !
var query= (from c in db.tblBlogs join a in db.tblBlogMedias on c.id
equals a.BlogId where c.id==db.tblBlogMedias.Max(p=>p.id)
select new
{}
If I understood, you want to get an object with specific fields of Blogs and a list of tblBlogMedias.
You could try this code:
var query2 = (from c in db.tblBlogs
orderby c.Id descending
group c.tblBlogMedias by new { c.Id, c.Name } into gb //It will show Id and name of tblBlogs, you can use more fields if you want
select new {
Id = gb.Key.Id,
Name = gb.Key.Name, //I don't know if you have this field, but you should change it
SecondTable = gb.ToList()
})
.OrderByDescending(o => o.Id) //this orderby with FirstOrDefault() replace where c.id==db.tblBlogMedias.Max(p=>p.id)
.FirstOrDefault();
I am learning ASP.net and I have come to the point that I want to insert, update, delete records in a database.
Currently I am trying to read out values out of 2 tables using "join" but when I display the results in a grid the Foreign Key values are still like : 2, 1, 2,... Instead I want them to be to coresponding words.
This is the current query I am using:
from p in dc.Personeels join a in dc.Afdelingens on p.fk_personeel_afdeling equals a.pk_afdeling_id select p
Does anyone know what I am doing wrong?
Try this query
var query = from p in dc.Personeels
join a in dc.Afdelingens on p.fk_personeel_afdeling equals a.pk_afdeling_id
select new
{
id = p.id, // your id from table dc.Personeels
name = a.name // Name from table dc.Afdelingens
} into x
select x;
I have a LINQ query (using with EF)
Basically I want to add a column in Select results based on value of another column.
I have PaymentDate column in DB table but not Paid column. If there is null in PaymentDate column it also shows payment is false and if it has some date in it means paid is true.
Here is my query, please guide me how to do that.
var selectedResults=
from InvoiceSet in Invoices
join BookedAreasSet in BookedAreas
on InvoiceSet.InvoiceID equals BookedAreasSet.InvoiceID
join AreaSet in Areas on BookedAreasSet.AreaID equals AreaSet.AreaID
select new {InvoiceSet.InvoiceNumber,InvoiceSet.Amount,InvoiceSet.TotalDiscount,InvoiceSet.GST, InvoiceSet.PaymentDate,InvoiceSet.ShoppingCentreID,BookedAreasSet.BookedAreaID,
AreaSet.Name,Here I want to add calculated value column based on InvoiceSet.PaymentDate value}
I think you should be able to do something like this
var selectedResults=
from InvoiceSet in Invoices
join BookedAreasSet in BookedAreas
on InvoiceSet.InvoiceID equals BookedAreasSet.InvoiceID
join AreaSet in Areas on BookedAreasSet.AreaID equals AreaSet.AreaID
select new { InvoiceSet.InvoiceNumber,InvoiceSet.Amount,InvoiceSet.TotalDiscount,InvoiceSet.GST,
InvoiceSet.PaymentDate,InvoiceSet.ShoppingCentreID,BookedAreasSet.BookedAreaID,
AreaSet.Name,Paid = (InvoiceSet.PaymentDate == null) }
I'm writing a query that looks like this:
var TheOutput = (from x in MyDC.MyTable
where.....
select new MyModel()
{
MyPropID = (from a in MyDC.MyOtherTable
where....
select a.ElementID).SingleOrDefault(),
MyPropData = (from a in MyDC.MyOtherTable
where a.ElementID == MyPropID
select a.ElementData).SingleOrDefault(),
}
I'm filling up MyModel with several properties from the database. Two of these properties are filled by reading another table. At the moment, I first read MyPropID to see if there's an element in the other table and then I read the other table again to get the data, regardless of whether or not an ID was retrieved.
How can I eliminate this second read if I know, from reading MyPropID and returning a null, that there's no data that matches the where a.ElementID == MyPropID clause.
Thanks.
var TheOutput = (from x in MyDC.MyTable
where.....
let id = (from a in MyDC.MyOtherTable
where....
select a.ElementID).SingleOrDefault()
select new MyModel()
{
MyPropID = id,
MyPropData = (from a in MyDC.MyOtherTable
where id != null && a.ElementID == id
select a.ElementData).SingleOrDefault()
}
If your code would create a single SQL statement from this query I do not think checking for null would matter. If this query would result in multiple SQL statements it might.