Linq query syntax to method query syntax - c#

Can anyone help with how to transform this LINQ query syntax to method syntax.
It is because i need to use the Take() method.
IQueryable<QueueItem> rs = from a in ldb.QueueItems
join b in ldb.Robots on a.RobotId equals b.Id
join c in ldb.RobotsXEnvironments on b.Id equals c.RobotId
join d in ldb.Environments on c.EnvironmentId equals d.Id
join e in ldb.Releases on d.Id equals e.EnvironmentId
where e.ProcessKey == dropdown || a.Reference == query ||
a.SpecificData.Contains(query) &&
a.StartProcessing >= fromDate && a.EndProcessing <= toDate
select a;

You can do something like this:
IQueryable<QueueItem> rs = (from a in ldb.QueueItems
join b in ldb.Robots on a.RobotId equals b.Id
join c in ldb.RobotsXEnvironments on b.Id equals c.RobotId
join d in ldb.Environments on c.EnvironmentId equals d.Id
join e in ldb.Releases on d.Id equals e.EnvironmentId
where e.ProcessKey == dropdown || a.Reference == query ||
a.SpecificData.Contains(query) &&
a.StartProcessing >= fromDate && a.EndProcessing <= toDate
select a).take(number);

Related

What's wrong with this LINQ?

I am using LINQ TO Entities & need to use Union operator.
This is my raw sql query.
(select DISTINCT c.DocumentId from [sDocument].[tDocumentStatus] c
inner join [sDocument].[tTOCStructure] d on c.DocumentId = d.FolderID
inner join [sDocument].[tAudit] e on c.DocumentId = e.FolderID
where d.FolderType = 2 and d.isDeleted = 0 and d.ClientID = 9 and e.AuditDescriptionID != 10)
Union
(select DISTINCT c.FolderID from [sDocument].[tTOCStructure] c
inner join [sDocument].[tAudit] e on c.DocumentId = e.FolderID
where c.FolderType = 2 and c.isDeleted = 0 and c.ClientID = 9 )
When I run the above sql, I get around 45 records. That's right as well
Below is LINQ for the same requirement.
IQueryable<DocumentListMapper> query = (
from c in entities.tDocumentStatus
join d in entities.tTOCStructures on c.DocumentId equals d.FolderID
join e in entities.tAudits on c.DocumentId equals e.FolderID
where d.FolderType == 2 && d.isDeleted == false && d.ClientID == clientId && e.AuditDescriptionID != 10
select new DocumentListMapper()
{
DocumentId = c.DocumentId,
DocumentName = d.CheckoutFolderName,
PublishDate = c.AssignedDate
}).Distinct().Union(
from c in entities.tTOCStructures
join e in entities.tAudits on c.FolderID equals e.FolderID
where c.FolderType == 2 && c.isDeleted == false && c.ClientID == clientId
select new DocumentListMapper()
{
DocumentId = c.FolderID,
DocumentName = c.CheckoutFolderName,
PublishDate = e.TaskDateTime
}).Distinct().OrderBy(x => x.PublishDate).Skip(pager * 50).Take(50);
But this LINQ returns more than 2500 records. This is not the desired records.
What's wrong in my LINQ??

SQL Query to LINQ syntax using not exist and join

My SQL query is like below working fine in SQL
I need to convert this to LINQ syntax
SQL-
SELECT [Key], Id
FROM LocalizationKeys AS lk
WHERE NOT EXISTS (SELECT 1
FROM Languages AS l
JOIN LocalizationValues AS lv ON l.Id = lv.LanguageId
WHERE l.Title = 'en-US' AND lv.LocalizationKeyId = lk.Id)
LINQ syntax I tried
var result =
(from lk in localizationKey
where !(from l in lang
join lv in localizationValue on l.Id equals lv.LanguageId
where l.Title == "en-US" && lv.LocalizationKeyId == lk.Id select 1).FirstOrDefault()
select lk).ToList();
Getting error:
Operator '!' cannot be applied to operand of type 'int'
Any clue where I made mistake?
You can try like this:
(from lk in localizationKey
where (from l in lang
join lv in localizationValue on l.Id equals lv.LanguageId
where (l.Title == "en-US" && lv.LocalizationKeyId == lk.Id)
select l).FirstOrDefault() == null
select lk).ToList();
or
(from lk in localizationKey
where !(from l in lang
join lv in localizationValue on l.Id equals lv.LanguageId
where !(l.Title == "en-US" && lv.LocalizationKeyId == lk.Id)
select l).FirstOrDefault().Any()
select lk).ToList();
Try this:
(from lk in localizationKey
where (from l in lang
join lv in localizationValue on l.Id equals lv.LanguageId
where !(l.Title == "en-US" && lv.LocalizationKeyId == lk.Id) select 1).FirstOrDefault()
select lk).ToList();
I think your original query is fine you just need to add another pair of brackets in the where clause:
(from lk in localizationKey
where !((from l in lang
join lv in localizationValue on l.Id equals lv.LanguageId
where l.Title == "en-US" && lv.LocalizationKeyId == lk.Id select 1).Any())
select lk).ToList();

Linq AND searches

I have a array called searchWords, that is a dynamic array that stores peoples search words. I need to add an option for AND search. So the search will only retrieve items if both variables in searchWords contains for resultList. Now it is searchWords.Any. Will searchWords.All make this works?
var resultList = from c in context.Category
join q in context.Question on c.CategoryId equals q.CategoryId
join qf in context.QuestionFilter on q.QuestionId equals qf.QuestionId
join a in context.Answer on q.QuestionId equals a.QuestionId into QuestAnsw
from a2 in QuestAnsw.DefaultIfEmpty()
orderby c.SortOrder
orderby q.SortOrder
where qf.FilterId == filterKeyAsInt
&& q.Published == true
&& c.Published == true
&& q.CustomerId == customerId
&& (searchWords.Any(w => a2.Text.Contains(w))
|| searchWords.Any(w => c.Text.Contains(w))
|| searchWords.Any(w => q.Text.Contains(w)))
select new { Category = c, Question = q };
You can put multiple clauses inside an All(), e.g.
&& (searchWords.All(w =>
a2.Text.Contains(w) &&
c.Text.Contains(w) &&
q.Text.Contains(w)))
...
You can do this if use searchWords.All, but i think searchWords.Any is more intuitive.
var resultList = from c in context.Category
join q in context.Question on c.CategoryId equals q.CategoryId
join qf in context.QuestionFilter on q.QuestionId equals qf.QuestionId
join a in context.Answer on q.QuestionId equals a.QuestionId into QuestAnsw
from a2 in QuestAnsw.DefaultIfEmpty()
orderby c.SortOrder
orderby q.SortOrder
where qf.FilterId == filterKeyAsInt
&& q.Published == true
&& c.Published == true
&& q.CustomerId == customerId
&& !
(
searchWords.All(w => !a2.Text.Contains(w))
&& searchWords.All(w => !c.Text.Contains(w))
&& searchWords.All(w => !q.Text.Contains(w))
)
select new { Category = c, Question = q };

GroupBy DayOfWeek in Linq

I have this Linq query which works fine
var test = (from c in context.ConsumptionSet
join pi in context.PropertiesInstanceSet on c.PropertiesInstanceID equals pi.PropertiesInstanceID
join ep in context.EquipmentPropertiesSet on pi.EquipmentPropertiesID equals ep.EquipmentPropertiesID
where (ep.EquipmentID == equipmentId && pi.ProprietesName == ProprietesName.Energy && c.Date <= DateTime.Today && c.Date >= EntityFunctions.AddDays(DateTime.Today, -7))
group c by c.Date.Day into grp
select new
{
test = grp.Key,
value = (from c2 in grp select c2.Value).Max()
}).ToList();
But I want to group these results by the DayOfWeek Property, and it seems like Linq doesn't allow it since I get this error when I replace group c by c.Date.Day by group c by c.Date.DayOfWeek :
The specified type member 'DayOfWeek' is not supported in LINQ to Entities
Is there any workaround to this problem ?
Use SqlFunctions.DatePart:
var test = (from c in context.ConsumptionSet
join pi in context.PropertiesInstanceSet on c.PropertiesInstanceID equals pi.PropertiesInstanceID
join ep in context.EquipmentPropertiesSet on pi.EquipmentPropertiesID equals ep.EquipmentPropertiesID
where (ep.EquipmentID == equipmentId && pi.ProprietesName == ProprietesName.Energy && c.Date <= DateTime.Today && c.Date >= EntityFunctions.AddDays(DateTime.Today, -7))
group c by SqlFunctions.DatePart("weekday", c.Date) into grp
select new
{
test = grp.Key,
value = (from c2 in grp select c2.Value).Max()
}).ToList();

How to join and select table data in mvc C#?

In my project I have services.
So in side the service I want to join tables and want to select more than one table data.
So I write this cording.
var query1 = from opv in _opvRepository.Table
join o in _orderRepository.Table on opv.OrderId equals o.Id
join g in _graduandRepository.Table on opv.graduand_id equals g.graduand_id
join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
join p in _productRepository.Table on pv.ProductId equals p.Id
where (opv.ceremony_id == ceremony_id) &&
(!o.Deleted) && (opv.IsHireItem == true) &&
(!p.Deleted) &&
(!pv.Deleted) && (opv.ceremony_id == ceremony_id)
select opv,g;
But there is error and I can't select opv and g. if I write select opv;it is ok. but i want to select both table.
How can i do it??
Try using anonymous types i.e.
query1 = from opv in _opvRepository.Table
join o in _orderRepository.Table on opv.OrderId equals o.Id
join g in _graduandRepository.Table on opv.graduand_id equals g.graduand_id
join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
join p in _productRepository.Table on pv.ProductId equals p.Id
where (opv.ceremony_id == ceremony_id) &&
(!o.Deleted) && (opv.IsHireItem == true) &&
(!p.Deleted) &&
(!pv.Deleted) && (opv.ceremony_id == ceremony_id)
select new { table1Val = opv,
table2Val = g
};

Categories

Resources