select p.ProduitNom,v.VonduDate,p.ProduitPrix from Produits p,Vondus v
where p.ProduitId = v.ProduitId and p.CentreId=1
How to do this request in entity framework ?
You can do that as shown below.
Inner Join :
from p in db.Produits
join v in db.Vondus on p.ProduitId equals v.ProduitId
where p.CentreId=1
select new {
ProduitNom = p.ProduitNom,
VonduDate = v.VonduDate,
ProduitPrix = p.ProduitPrix
}
If you would like to learn,you can refer this : Queries in LINQ to Entities
You can use Join:
EDIT:
You should have a context to connect with Database first, or else, at least 2 lists:
List<Produits> Produits = new List<Produits>();
List<Vondus> Vondus = new List<Vondus>();
Then using below lambda expression:
var res = Produits.Join(Vondus, p => p.ProduitId, v => v.ProduitId,
(p, v) => new { p, v })
.Where(pv => pv.p.ProduitId == pv.v.ProduitId && pv.p.CentreId == 1)
.Select(pv => new { pv.p.ProduitNom, pv.v.VonduDate, pv.p.ProduitPrix)
.ToList();
The res will be a list containts of ProduitNom, VonduDate and ProduitPrix
Related
I have a table with the following fields:
Id Name Score Event
(1 John 2 3)
(2 John 4 3)
(3 john 5 3)
and I would like to get the following result:
(John, "2 +4+5", 11(Total score))
I'm trying to combine the results to show the sum in a string format then show the sum result.
Can anyone please help me with this issue.
Thank you,
Franciso
You need to call the toList() method before using methods like string.Format or string.Join because they are not supported by Linq to SQL.
You should modify your code into something like this:
var group = (from Classificacao in _dbSet.classificacao
from Evento in _dbSet.evento
where Classificacao.class_id_Evento == id_evento
where Evento.id_evento == id_evento
group Classificacao by new
{
Classificacao.class_atletaNome,
Classificacao.class_numAtleta,
Classificacao.class_atletaEquipa,
Classificacao.class_paisAtleta,
Evento.even_name,
Evento.local.local_nome,
Evento.compTipo.CompTipo_name
} into g).ToList()
The idea is to materialize the query into a list before calling the methods.
var model = group.Select(g => new
{
g.Key.class_numAtleta,
g.Key.class_atletaEquipa,
g.Key.class_atletaNome,
g.Key.class_paisAtleta,
class_fianlAtleta = g.Sum(c => c.class_fianlAtleta.Value),
class_classGeral = g.Min(c => c.class_fianlAtleta.Value),
class_nome = g.Key.even_name,
local_nome = g.Key.local_nome,
CompTipo_name = g.Key.CompTipo_name,
class_40ponto = ((from u in g
where u.class_id_Evento == id_evento
group u by new
{
u.class_atletaNome,
u.class_fianlAtleta
}
into k
select new
{
//Score = string.Concat(k.Key.class_fianlAtleta.ToString(), " + ", g.Key.class_atletaNome)
Score = string.Format("{0}",k.Key.class_fianlAtleta)
})).Select(c=>c.Score).FirstOrDefault()
}).OrderBy(c => c.class_fianlAtleta).ThenBy(c => c.class_classGeral)).toList();
I can't check if the code is correct therefore I leave it to you, anyway once you have your List in memory you can apply the methods supported by Linq to entity.
Here is my code:
var model = (from Classificacao in _dbSet.classificacao
from Evento in _dbSet.evento
where Classificacao.class_id_Evento == id_evento
where Evento.id_evento == id_evento
group Classificacao by new
{
Classificacao.class_atletaNome,
Classificacao.class_numAtleta,
Classificacao.class_atletaEquipa,
Classificacao.class_paisAtleta,
Evento.even_name,
Evento.local.local_nome,
Evento.compTipo.CompTipo_name
} into g
select new
{
g.Key.class_numAtleta,
g.Key.class_atletaEquipa,
g.Key.class_atletaNome,
g.Key.class_paisAtleta,
class_fianlAtleta = g.Sum(c => c.class_fianlAtleta.Value),
class_classGeral = g.Min(c => c.class_fianlAtleta.Value),
class_nome = g.Key.even_name,
local_nome = g.Key.local_nome,
CompTipo_name = g.Key.CompTipo_name,
class_40ponto = ((from u in g
where u.class_id_Evento == id_evento
group u by new
{
u.class_atletaNome,
u.class_fianlAtleta
}
into k
select new
{
//Score = string.Concat(k.Key.class_fianlAtleta.ToString(), " + ", g.Key.class_atletaNome)
Score = string.Format("{0}",k.Key.class_fianlAtleta)
})).Select(c=>c.Score).FirstOrDefault()
}).OrderBy(c => c.class_fianlAtleta).ThenBy(c => c.class_classGeral);de here
You need something like this:
var result = table
.GroupBy(t => new { t.Name, t.Event })
.ToList()
.Select(t => new
{
Name = t.Key.Name,
JoinedScores = string.Join(" + ", t.Select(group => group.Score).ToArray()),
TotalScore = t.Sum(group => group.Score).ToString() + " (Total Score)"
}).ToList();
The first call to the ToList() method is needed because Linq to SQL will not support the calls to string.Join(). I'm not sure but probably you can replace it with a call to AsEnumerable().
I can use LINQ's Join with Lambda notations no problem, but I can't work out how one would then add a where condition.
var q = query.Join(context.CustomerIds,
x => x.CustomerId,
y => y.CustomerId,
(x, y) => new CustomerLookupResult()
{
dob = x.DateOfBirth.ToString(),
forenames = x.Forenames,
surname = x.Surname,
loyaltyNo = y.Identifier,
customerId = x.CustomerId
});
The table I'm joining the first to contains the loyaltyNo in its Identifier column, but also contains other information in the same column and so uses a second column IdentifierTypeCode to allow filtering.
So how do I now add .Where(x => x.IdentifierTypeCode == "LOYALTY") like I would in SQL. Appending this to end refers to the new object.
You could apply your Where before doing the join.
var q = customerLoyalties
.Where(x => x.IdentifierTypeCode == "LOYALTY")
.Join(customers,
x => x.CustomerId,
y => y.CustomerId,
(x, y) => new CustomerLookupResult()
{
CustomerId = y.CustomerId,
Name = y.Name,
IdentifierTypeCode = x.IdentifierTypeCode
});
You can also use this way to achieve that using Linq.
var match = from t1 in context.orders
join t2 in context.orderdetails on
new { t1.OrderID } equals
new { t2.OrderID }
join t3 in context.products on
new { t2.ProductID } equals
new { t3.ProductID }
where t3.ProductID == id
select t3;
return match.ToList();
The first parameter to the Join takes any IEnumerable, so you can apply the Where at that point, or earlier
var q = query.Join(context.CustomerIds.Where(x=>x.IdentifierTypeCode=="LOYALTY"),
x => x.CustomerId,
y => y.CustomerId,
(x, y) => new CustomerLookupResult()
{
dob = x.DateOfBirth.ToString(),
forenames = x.Forenames,
surname = x.Surname,
loyaltyNo = y.Identifier,
customerId = x.CustomerId
});
alternatively, if you don't like to put too much on one line:
var filteredLoyalties = context.CustomerIds.Where(x=>x.IdentifierTypeCode=="LOYALTY");
var q = query.Join(filteredLoyalties,
...
I want to return multiple column in join result and fill in view model(VMFoodFoodMeal).
for example I want to fill VMFoodFoodMeal by Join result
Thanks
IEnumerable<VMFoodFoodMeal> _fmt = (from e in db.FoodProgramMealFood
join j in db.Foods on e.FoodId equals j.Id
select new
{
Id = e.Id,
Name = j.Name,
});
It is hard to understand what you want, but as example you can look here at Group Join section.
Here is an example code from there:
public void Linq103()
{
string[] categories = new string[]{
"Beverages",
"Condiments",
"Vegetables",
"Dairy Products",
"Seafood" };
List<Product> products = GetProductList();
var q =
from c in categories
join p in products on c equals p.Category into ps
select new { Category = c, Products = ps };
foreach (var v in q)
{
Console.WriteLine(v.Category + ":");
foreach (var p in v.Products)
{
Console.WriteLine(" " + p.ProductName);
}
}
}
result:
Beverages:
Chai
Chang
Guaraná Fantástica
Sasquatch Ale
Steeleye Stout
Côte de Blaye
Chartreuse verte
Ipoh Coffee
...
if you want to send your join Result to a model without using foreach statement you have to use this code
IEnumerable<VMFoodFoodMeal> _fmt = (from e in db.FoodProgramMealFood
join j in db.Foods on e.FoodId equals j.Id
select new VMFoodFoodMeal()
{
Id = e.Id,
Name = j.Name,
});
for example in above code I had a view model (VMFoodFoodMeal) and want to fill it by join result.
I have to make new instance of my model in select and then fill its property.
Thanks
I want to select all the records where FeeZoneID doesn't exist in second linq output.
I know it can easily done in SQL using NOT IN(....) but i do it in LINQ???
from d in FeeZones
where d.FeeZoneID !(from b in FeeZoneSchemes
where b.FeeSchemeID == 1
join c in FeeZones on b.FeeZoneID equals c.FeeZoneID
select c)
select d
In my C# class this is how I am doing :
public IList<FeeZone> _LQ_GetAllAvaliableFeeZoneForGivenFeeSchemeID(int FeeSchemeID)
{
using(var db = new QualificationContext())
{
var query = (from d in db.FeeZone
where (!(from b in db.FeeZoneScheme
where b.FeeSchemeID == FeeSchemeID
join c in db.FeeZone on b.FeeZoneID equals c.FeeZoneID
select c).Contains(d.FeeZoneID))
select d).ToList();
return query.ToList();
}
}
And this is the error :
Many Thanks
Try this code:
var query = from d in FreeZones
let unwantedZonesIds = FeeZoneSchemes.Where(b => b.FreeSchemeID == 1)
.Select(b => b.FreeZoneID)
where !unwantedZonesIds.Contains(d.FreeZoneID)
select d;
Use Any instead of Contains
var query = (from d in db.FeeZone
where (!((from b in db.FeeZoneScheme
where b.FeeSchemeID == FeeSchemeID
join c in db.FeeZone on b.FeeZoneID equals c.FeeZoneID
select c).Any(x => x.FeeZoneID ==d.FeeZoneID))
select d).ToList();
Please try is as below.
from d in FeeZones
where (!(from b in FeeZoneSchemes where b.FeeSchemeID == 1
join c in FeeZones on b.FeeZoneID equals c.FeeZoneID
select c.FeeZoneID).Contains(d.FeeZoneID))
select d
You can use the following query .
IList<FeeZones> lst = new List<FeeZones>() { new FeeZones() { FeeZoneID = 1 }, new FeeZones() { FeeZoneID =2 } };
IList<FeeZoneSchemes> lstnew = new List<FeeZoneSchemes>(){new FeeZoneSchemes(){FeeSchemeID=1,FeeZoneID=1}};
var res = (from d in lst
where !(
from c in lstnew
join f in lst on c.FeeZoneID equals f.FeeZoneID
where c.FeeSchemeID == 1 && c.FeeZoneID == f.FeeZoneID
select f.FeeZoneID
).Contains(d.FeeZoneID)
select d).ToList();
or You can use the Sql way of exists query like below.
var res = (from d in lst
where !(
from c in lstnew
where c.FeeSchemeID == 1 && c.FeeZoneID == d.FeeZoneID
select c
).Any()
select d).ToList();
I would like to end up with a list where my categories are grouped according to the users id.
IEnumerable<JoinClass> catList =
from c in db.Users2
join e in db.Categories on c.Id_Users equals e.FK_Users
where c.EEID == UserEEID
group e.Category by c.EEID in z
select new JoinClass
{
Category = e.Category,
EEID = c.EEID,
};
return View(catList.ToList() );
IEnumerable<JoinClass> catList =
from c in db.Users2
join e in db.Categories on c.Id_Users equals e.FK_Users
where c.EEID == UserEEID
group e.Category by c.EEID in z
select new JoinClass
{
Categories = z.ToList(),
EEID = z.Key,
};
return View(catList.ToList() );
Assuming you ACTUALLY meant to find all Categories for each EEID.