This is my XML content:
<ListEnginsMesures>
<EnginsESC>
<NomValide>Engin_inconnu</NomValide>
<NomEquivalents>
<NomEquivalent>Engin inconnu</NomEquivalent>
<NomEquivalent>Engininconnu</NomEquivalent>
</NomEquivalents>
</EnginsESC>
<EnginsESC>
<NomValide>DRSC_6150</NomValide>
<NomEquivalents>
<NomEquivalent>DRSC 6150</NomEquivalent>
<NomEquivalent>DRSC6150</NomEquivalent>
<NomEquivalent>DRSC6.150</NomEquivalent>
<NomEquivalent>DRSC_6.150</NomEquivalent>
</NomEquivalents>
</EnginsESC>
<ListEnginsMesures>
I want to select value of 'NomValide' when i have 'NomEquivalent'.
For example :
(select 'NomValide' where 'NomEquivalent' = "Engin inconnu") will return Engin_inconnu.
(select 'NomValide' where 'NomEquivalent' = "DRSC_6.150") will return DRSC_6150.
HOW CAN I ACHIEVE THIS ONE ?
THANKS IN ADVANCE..
If you expect only one result then the following should work:
string xml=#"<ListEnginsMesures>
<EnginsESC>
<NomValide>Engin_inconnu</NomValide>
<NomEquivalents>
<NomEquivalent>Engin inconnu</NomEquivalent>
<NomEquivalent>Engininconnu</NomEquivalent>
</NomEquivalents>
</EnginsESC>
<EnginsESC>
<NomValide>DRSC_6150</NomValide>
<NomEquivalents>
<NomEquivalent>DRSC 6150</NomEquivalent>
<NomEquivalent>DRSC6150</NomEquivalent>
<NomEquivalent>DRSC6.150</NomEquivalent>
<NomEquivalent>DRSC_6.150</NomEquivalent>
</NomEquivalents>
</EnginsESC>
</ListEnginsMesures>";
var xe = XElement.Parse(xml);
var result = xe.Elements("EnginsESC")
.Where
(
x=>
x.Element("NomEquivalents")
.Elements("NomEquivalent")
.Any(n=>(string)n=="Engin inconnu")
)
.Select(x=>(string)x.Element("NomValide"))
.FirstOrDefault();
If you expect more than one results:
var results = xe.Elements("EnginsESC")
.Where
(
x=>
x.Element("NomEquivalents")
.Elements("NomEquivalent")
.Any(n=>(string)n=="Engin inconnu")
)
.Select(x=>(string)x.Element("NomValide"));
Related
I am close but no cigar. In SQL I can use an ISNULL in my where clause but I can't seem to get it to pass in linq.
var q3 =(
from Prin in HR
.Where(Prin => (Prin.JobName == "Pricipal-Elementary") &&
(ASup =>((String.Compare(ASup.UnitName,null) >=0)&&
(String.Compare(ASup.UnitName ,"%Learning%"))
))
)
//WHERE Prin.JobName = 'Principal-Elementary'
//AND ISNULL(ASup.UnitName,'') LIKE '%Learning%'
//ORDER BY SchoolName
from ASup in HR
.Where(ASup => ASup.ADAccount == Prin.ChiefADAccount)
.DefaultIfEmpty()
from Sch in UnitToSchools
.Where(Sch => Sch.UnitCode == Prin.UnitCode)
.DefaultIfEmpty()
select new
{
SchoolName = Prin.UnitName
,SchoolId = Sch.SchoolDetailFCSId
,PrincipalID = Prin.ADAccount
,LComm = ASup.UnitName
,AreaSupId = Prin.ChiefADAccount
}
);
var xyz = (q3).ToList();
//Below is the correct query in SQL
SELECT Prin.UnitName AS SchoolName
, Sch.SchoolDetailFCSId AS SchoolId
, Prin.ADAccount AS PrincipalID
, ASup.UnitName AS LComm
, Prin.ChiefADAccount AS AreaSupID
FROM IP_F.dbo.HR Prin
LEFT OUTER JOIN IP_F.dbo.HR ASup
ON ASup.ADAccount = Prin.ChiefADAccount
LEFT OUTER JOIN IP_F.dbo.UnitToSchool Sch
ON Sch.UnitCode = Prin.UnitCode
WHERE Prin.JobName = 'Principal-Elementary'
AND ISNULL(ASup.UnitName,'') LIKE '%Learning%'
ORDER BY SchoolName
Any help would be appreciated. Apparently I can't use String.Compare on a lambda. So I am stuck...
Thanks in Advance.
This part of your query:
ISNULL(ASup.UnitName,'') LIKE '%Learning%'
would be written in LINQ as
(ASup.UnitName ?? '').Contains("Learning")
Have your tried something like?
(String.IsNullOrEmty(ASup.UnitName) ? string.Empty : ASup.UnitName).Contains("Learning")
I have List List courseTakenList.i want to filter out the Status which are in Completed,Dropped,Current,Schudeled. Here is the code sample.
courseTakenList = (from courseTaken in courseTakenList
select new Course
{
Status = "Scheduled"?"COMPLETE"?"Dropped"? "Current"
}).ToList();
You could do it even more concise
var statuslist= {"Completed","Dropped","Current","Schudeled"};
var courses = courseTakenList.Where(courseTaken =>
statuslist.Contains(courseTaken.Status);
Linq In query like this will resolve your issue
var statuslist= {"Completed","Dropped","Current","Schudeled"};
var query = from courseTaken in courseTakenList
where statuslist.Contains( courseTaken .Status )
select courseTaken ;
Note : change select clause as you want
string popUpHTML="";
var xx = from Temp in TemplateList
where
(
Temp.TitleID == titleID
)
select Temp.HTML;
foreach (var s in xx)
{
popUpHTML = s.ToString();
}
The code above is working. The executed linq is supposed to return only one value. Is there any way I can get the result of the above LINQ without a foreach loop. (Sorry I am new to LINQ).
Update: titleID is a unique key in the database, hence only one result is expected.
Right now your query returns an IEnumerable, instead use FirstOrDefault() (or Single() if you are absolutely sure that there always will be exactly one result):
var popUpHTML = (from Temp in TemplateList
where Temp.TitleID == titleID
select Temp.HTML).FirstOrDefault();
var popUpHTML = TemplateList.Where(temp => temp.TitleID == titleID)
.Select(temp => temp.HTML)
.SingleOrDefault();
popUpHtml is null if there doesn't exist an item with the provided ID.
string popUpHTML =
(
from Temp in TemplateList
where ( Temp.TitleID == titleID )
select Temp.HTML
)
.FirstOrDefault();
var popUpHTML = TemplateList.FirstOrDefault(Temp => Temp.TitleID == titleID);
var xx = (from Temp in TemplateList
where
(
Temp.TitleID == titleID
)
select Temp.HTML).Take(1);
I have this simple query:
SELECT xObjectID, xObjectName
FROM dbo.xObject
where CONTAINS( xObjectRef, '1838 AND 238671')
Which i am trying to convert to linq but i can't get it to work,
And it's driving me up the wall.
Thanks!
Fulltext searches are not compatible with linq to sql. You will have to call a stored procedure.
Edit:
Or do you want a linq query that will return the same result set as the sql?
Does this work for you? This does require xObjectRef to be a property of xObject.
from obj in dbo.xObject
where obj.xObjectRef.Contains("1838") && obj.xObjectRef.Contains("238671")
select new { xObjectId = obj.xObjectId, xObjectName = obj.xObjectName}
var query = from c in context.xObject
where c.xObjectRef.Contains("1838") && c.xObjectRef.Contains("238671")
select new { ObjectID = c.xObjectID, ObjectName = c.xObjectName };
var a = xObject.where(n=>n.Contains("1838") && n.Contains("238671") )).
Select(s=>new {xObjectID=s.xObjectID , xObjectName=s.xObjectName});
I have an SQL table with the following design:
TableSchedule:
Id
Description
ImportedDate
I can import a large number of items and they will all have the same ImportedDate.
How can I write a LINQ query that grabs only the entries with the most recent ImportedDate?
var ResultSchedule =
from a in Db.TableSchedule
where a.ImportedDate == (Newest?)
Try This
var ResultSchedule =
from a in Db.TableSchedule
where a.ImportedDate == (from item in DB.TableSchedule Select item.ImportedDate).Max()
Try this:
Select * From Table
Where ImportedDate =
(Select max(ImportedDate) From Table)
If you need the latest record for each [something] like, say, for each individual customer, then make the siubquery correlated
Select * From Table t
Where ImportedDate =
(Select max(ImportedDate)
From Table
Where Customer = t.Customer)
How about this:
var ResultSchedule = from a in Db.TableSchedule
let max = Db.TableSchedule.Max(i => i.ImportedDate)
where a.ImportedDate == max
select a;
You can group the results and order them by date:
var ResultsSchedule = Db.TableSchedule.
GroupBy(a => a.ImportedDate).
OrderByDescending(g => g.Key).
FirstOrDefault().
Select(g => g);
In fluent syntax:
var ResultSchedule =
Db.TableSchedule.Where(
a => a.ImportedDate.Equals(Db.TableSchedule.Max(b => b.ImportedDate))
);