Syntax for "and" and distinct operation in entity framework? - c#

I want to display the records whose accountid, id are matching and also display distinct records.
var result = from a in cxt.tblInventoryProducts
where a.aid == accID && a.id == id
select new
{
a.productType,
a.productName
};
Thanks in advance

Something like
var result = from a in cxt.tblInventoryProducts
where a.aid == accID && a.id == id
group a by new { a.productType, a.productName } into grp
select new
{
grp.Key.productType,
grp.Key.productName
};

Related

LINQ query doesn't return the single value

I have this query and I want to return a single value which is the I.Id and I have a parameter which is filled with an Id that is sent from a Controller to the Dao(Where the Linq query is nested).
The problem is, that this query isn't just returning a value, is returning every fields in the table that this query produces and I just want the Id.
This is the query:
public InscriptionDetail GetInscriptionIdByPersonId(int id)
{
using (var db = new HIQTrainingEntities())
{
var inscription =
from i in db.Inscriptions
join c in db.Certifications on i.PersonId equals c.PersonId
where i.PersonId == id
select new InscriptionDetail
{
Id = i.Id,
};
return inscription.FirstOrDefault();
}
}
Can someone help me with this query ?
What's wrong ?
Your return type is of InscriptionDetail. If it is only the int id you need to return, why not change to the following?
public int GetInscriptionIdByPersonId(int personId)
{
using (var db = new HIQTrainingEntities())
{
var inscription =
from i in db.Inscriptions
join c in db.Certifications on i.PersonId equals c.PersonId
where i.PersonId == personId
select i.Id;
return inscription.FirstOrDefault();
}
}

How to pull one column from second table in Linq query with join

I have the following linq query that works fine, but I am wanting to pull one column (CompanyId) from context.Emps into the results along with the results from context.BillingProfiles. How would I modify the select (select prof) below to include said column?
var query = (from prof in context.BillingProfiles
join emp in context.Emps on prof.ID equals emp.ID
join grp in context.BillingGroups on prof.GroupID equals grp.GroupID
where (prof.EndDate == null) && (grp.System == "sysGrp") && (prof.ID == id)
select prof).Distinct()
.Select(x => new OpId()
{
id = x.ID,
GroupId = x.GroupID,
OpId = x.OpID,
StartDate = x.StartDate,
EndDate = x.EndDate,
AddedOn = x.AddedOn,
AddedBy = x.AddedBy,
RemovedOn = x.RemovedOn,
RemovedBy = x.RemovedBy,
Prodid = x.ProdID,
});
Thanks
Project an anonymous object containing those too:
var query = from prof in context.BillingProfiles
join emp in context.Emps on prof.ID equals emp.ID
join grp in context.BillingGroups on prof.GroupID equals grp.GroupID
where prof.EndDate == null && prof.ID == id && grp.System == "sysGrp"
select new { prof, emp.CompanyId, grp };

Linq To Sql Sub Query Complex

tblItem
Name
ProductID
tblProduct
Name
ProductID
CategoryID
tblCategory
Name
CategoryID
var itms = from item in CMP.tblItems
let t2s = (from g in CMP.tblProducts
where g.CategoryID==CatID
select g.ProductID)
where item.Name.Contains(Model) && item.ProductID.ToString() == t2s.ToString()
select new { item.Name };
My problem is more than one product is return to t2s (Sub-Query). if i add FirstOrDefault() to Sub-Query then it will match it with only one product id! i need to match will all productid(s) it returns.
Try This One:
var itms=from item in CMP.tblItems
from g in CMP.tblProducts
where item.Name.Contains(Model) && item.ProductID == g.ProductID && g.CategoryID == CatID
select new {item.Name};
Use the navigation properties that LINQ-to-SQL creates for you. Item should have property Product. So you can simply do this:
var itms = from item in CMP.tblItems
where item.Name.Contains(Model) && item.Product.CategoryID = CatId
select new { item.Name };

Select Values from two tables using Linq

Hi I know this has been asked plenty of times, I'm not getting it through my skull
How to select Values from several tables
I made these two Linq queries
First
r = (from d in db.stageManagers
where d.profileID == UserID && d.verticalID == VerticalID
select new StageModels()
{
UserId = d.profileID,
VerticalId = (int)d.verticalID,
VerticalStageID = d.stageID
}).FirstOrDefault();
Second
r = (from d in db.stageManagerVerticals
where d.ID == r.VerticalId
select new StageModels()
{
VerticalName = d.verticalName
}
).FirstOrDefault();
I want to make them one statement since they add data to one model and the tables they query have a Pk Fk relationship
In the first block d.verticalId is what I use to get the value(Name) in the secondblock, d.verticalId is primary key to stageManagerVerticals and foreign key in stageManager, How can i join these queries ?
I tried this:
r = (from d in db.stageManagers
join c in db.stageManagerVerticals on d.stageID equals c.ID
where d.profileID == UserID && d.verticalID == VerticalID
select new StageModels()
{
UserId = d.profileID,
VerticalId = (int)d.verticalID,
VerticalStageID = d.stageID;
VerticalName = c.verticalName
}
).FirstOrDefault();
try with JOIN in LINQ to select values from more than one table
eg:
var innerJoinQuery =
from category in categories
join prod in products on category.ID equals prod.CategoryID
select new { ProductName = prod.Name, Category = category.Name };
var leftOuterJoin=(c in categories
join p in products on c.ID equals p.CategoryID into t
from temp in t.DefaultIfEmpty()
select new { ProductName = p.Name, Category = c.Name }
).ToList();
You can utilize the Navigational Properties,
var r = db.stageManagers.Where(x => x.profileID == UserID && x.verticalID == VerticalID).
Select(
d => new StageModels() {
UserID = d.profileID,
VerticalID = (int)d.verticalID,
VerticalStageID = d.stageID,
VerticalName = d.stageManagerVertical.verticalName
}
).FirstOrDefault();

Select a field that is not in Group By clause in LINQ

could anybody help me about
"How to select a field that is not in Group By clause in LINQ"
var ReportData =
from a in context.Table1
from b in context.Table2
where a.personelID == b.ID
&& a.DateField.Value.Year == 2011
group a by new { a.personelID, b.Name, b.Surname} into grouping
select new
{
// grouping.Key.DateField,
Name= grouping.Key.Name,
Surname= grouping.Key.Surname,
PagaBruto = grouping.Sum(i => i.Bruto)),
};
I can't select the field "DateField" that is in Table1, I don't want to have this field in Group By, because I will get a wrong result.
THANKS!
I think you need to select both table members before you group by so you can select data from both. I did a join instead of your where clause.
(from a in context.Table1
join b in context.Table2 on a.PersonalID equals b.ID
select new { A=a, B=b } into joined
group joined by new { joined.A.PersonalID, joined.B.Name, joined.B.Surname } into grouped
select grouped.Select(g => new {
DateField= g.A.Datefield,
Name = g.B.Name,
Surname = g.B.Surname,
PagaBruto = g.A.Sum(i => i.Bruto)
})).SelectMany(g => g);
Maybe this will work?
group a by new { a.personelID, b.Name, b.Surname, a.DateField} into grouping
select new
{
DateField = grouping.Key.DateField,
Name= grouping.Key.Name,
Surname= grouping.Key.Surname,
PagaBruto = grouping.Sum(i => i.Bruto)),
};

Categories

Resources