Next Linq Question
-
I have this method:
public static List QueryInventoryParts(List<> SearchTerms, int ProducId = 0)
{
using (SparesDataContext context = getDataContext())
{
List<> retVal = new List<>();foreach (SearchItemModel searchModel in SearchTerms) { List<\> results = (from pp in context.mmlProductsParts join pa in context.tblParts on pp.PartId equals pa.PartId where pa.PartNumber.Contains(searchModel.SearchTerm) || pa.Keyword.Contains(searchModel.SearchTerm) select new PartModel { PartId = pa.PartId, Category = GetCategory(pa.CategoryId), PartNumber = pa.PartNumber, Keywords = pa.Keyword, PartDescription = pa.Description, Price = pa.Price.Value, Ratio = pa.Ratio.Value, Tribal = pa.Tribal }).ToList(); foreach (var result in results) { retVal.Add(result); } } return retVal; }
}
If the ProductId is passed in, then I was to add "&& pp.ProductId = ProductId" to the WHERE clause, however, if Zero is passed in, I want to return all products. I'm not sure of the syntax on this. Can someone show me? Thanks
Everything makes sense in someone's mind
-
I have this method:
public static List QueryInventoryParts(List<> SearchTerms, int ProducId = 0)
{
using (SparesDataContext context = getDataContext())
{
List<> retVal = new List<>();foreach (SearchItemModel searchModel in SearchTerms) { List<\> results = (from pp in context.mmlProductsParts join pa in context.tblParts on pp.PartId equals pa.PartId where pa.PartNumber.Contains(searchModel.SearchTerm) || pa.Keyword.Contains(searchModel.SearchTerm) select new PartModel { PartId = pa.PartId, Category = GetCategory(pa.CategoryId), PartNumber = pa.PartNumber, Keywords = pa.Keyword, PartDescription = pa.Description, Price = pa.Price.Value, Ratio = pa.Ratio.Value, Tribal = pa.Tribal }).ToList(); foreach (var result in results) { retVal.Add(result); } } return retVal; }
}
If the ProductId is passed in, then I was to add "&& pp.ProductId = ProductId" to the WHERE clause, however, if Zero is passed in, I want to return all products. I'm not sure of the syntax on this. Can someone show me? Thanks
Everything makes sense in someone's mind
-
I have this method:
public static List QueryInventoryParts(List<> SearchTerms, int ProducId = 0)
{
using (SparesDataContext context = getDataContext())
{
List<> retVal = new List<>();foreach (SearchItemModel searchModel in SearchTerms) { List<\> results = (from pp in context.mmlProductsParts join pa in context.tblParts on pp.PartId equals pa.PartId where pa.PartNumber.Contains(searchModel.SearchTerm) || pa.Keyword.Contains(searchModel.SearchTerm) select new PartModel { PartId = pa.PartId, Category = GetCategory(pa.CategoryId), PartNumber = pa.PartNumber, Keywords = pa.Keyword, PartDescription = pa.Description, Price = pa.Price.Value, Ratio = pa.Ratio.Value, Tribal = pa.Tribal }).ToList(); foreach (var result in results) { retVal.Add(result); } } return retVal; }
}
If the ProductId is passed in, then I was to add "&& pp.ProductId = ProductId" to the WHERE clause, however, if Zero is passed in, I want to return all products. I'm not sure of the syntax on this. Can someone show me? Thanks
Everything makes sense in someone's mind
Hi, probably not the best solution but off the top of my head i can think of 2 methods. 1. Use a switch on ProductID - case 0: --> do query to return all default: --> do other query to return specifics matching ProductId 2.
List<< PartModel>> results = **((ProductId != 0) ? ** (from pp in context.mmlProductsParts
join pa in context.tblParts on pp.PartId equals pa.PartId
where pa.PartNumber.Contains(searchModel.SearchTerm) ||
pa.Keyword.Contains(searchModel.SearchTerm)
&& pp.ProductId == ProductId
select new PartModel
{
PartId = pa.PartId,
Category = GetCategory(pa.CategoryId),
PartNumber = pa.PartNumber,
Keywords = pa.Keyword,
PartDescription = pa.Description,
Price = pa.Price.Value,
Ratio = pa.Ratio.Value,
Tribal = pa.Tribal
}).ToList()
: (from pp in context.mmlProductsParts
join pa in context.tblParts on pp.PartId equals pa.PartId
where pa.PartNumber.Contains(searchModel.SearchTerm) ||
pa.Keyword.Contains(searchModel.SearchTerm)
select new PartModel
{
PartId = pa.PartId,
Category = GetCategory(pa.CategoryId),
PartNumber = pa.PartNumber,
Keywords = pa.Keyword,
PartDescription = pa.Description,
Price = pa.Price.Value,
Ratio = pa.Ratio.Value,
Tribal = pa.Tribal
}