程序从 LINQ 搜索中没有返回任何内容,仅返回 0(最终 = 0)。我使用了调试器,所以我认为问题出在 linq 类中,因为程序停在单词“where”处。我不明白哪里有错误。将非常感激。
if (checkLINQ.IsChecked == true)
{
strategy = new LINQ();
Final = strategy.Search(_scientist, _filePath);
}
public class LINQ : IStrategy
{
public LINQ() { }
public ObservableCollection<Scientist> Search(Scientist s, string path)
{
ObservableCollection<Scientist> _s = new ObservableCollection<Scientist>();
var doc = XDocument.Load(path);
var match = (from value in doc.Descendants("Scientist")
where
(s.Name == null || s.Name == value.Element("Name")?.Value) &&
(s.AuthorName == null || s.AuthorName == value.Element("AuthorName")?.Value) &&
(s.Faculty == null || s.Faculty == value.Element("Faculty")?.Value) &&
(s.AuthorPosition == null || s.AuthorPosition == value.Element("AuthorPosition")?.Value) &&
(s.Gender == null || s.Gender == value.Element("Gender")?.Value) &&
(s.Age == null || == value.Element("Age")?.Value)
select value).ToList();
foreach (var obj in match)
{
Scientist sc = new Scientist();
sc.Name = obj.Element("Name")?.Value ?? string.Empty;
sc.AuthorName = obj.Element("AuthorName")?.Value ?? string.Empty;
sc.Faculty = obj.Element("Faculty")?.Value ?? string.Empty;
sc.AuthorPosition = obj.Element("AuthorPosition")?.Value ?? string.Empty;
sc.Gender = obj.Element("Gender")?.Value ?? string.Empty;
sc.Age = obj.Element("Age")?.Value ?? string.Empty;
_s.Add(sc);
}
return _s;
}
}
请尝试以下解决方案。
c#
void Main()
{
var result = Search(new Scientist
{
Name = "Роботи сучасності",
Age = "78"
}, "https://raw.githubusercontent.com/Yuliia-1102/Lab-xml-linq-/main/Labaratory_2/Scientists.xml");
Console.WriteLine(result);
}
public static List<Scientist> Search(Scientist s, string path)
{
var doc = XDocument.Load(path);
List<Scientist> Scientists = doc.Descendants("Scientist")
.Where(x => x.Descendants("Name").Any(c => c?.Value == s.Name)
|| x.Descendants("AuthorName").Any(c => c?.Value == s.AuthorName)
|| x.Descendants("Faculty").Any(c => c?.Value == s.Faculty)
|| x.Descendants("AuthorPosition").Any(c => c?.Value == s.AuthorPosition)
|| x.Descendants("Gender").Any(c => c?.Value == s.Gender)
|| x.Descendants("Gender").Any(c => c?.Value == s.Gender)
|| x.Descendants("Age").Any(c => c?.Value == s.Age))
.Select(x => new Scientist()
{
Name = x.Element("Name")?.Value,
AuthorName = x.Element("AuthorName")?.Value,
Faculty = x.Element("Faculty")?.Value,
AuthorPosition = x.Element("AuthorPosition")?.Value,
Gender = x.Element("Gender")?.Value,
Age = x.Element("Age")?.Value
}).ToList();
return Scientists;
}
[XmlRoot(ElementName = "Scientist")]
public class Scientist
{
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "AuthorName")]
public string AuthorName { get; set; }
[XmlElement(ElementName = "Faculty")]
public string Faculty { get; set; }
[XmlElement(ElementName = "AuthorPosition")]
public string AuthorPosition { get; set; }
[XmlElement(ElementName = "Gender")]
public string Gender { get; set; }
[XmlElement(ElementName = "Age")]
public string Age { get; set; }
}
输出