score:1
Accepted answer
You can just pass in null if the property is null:
var xml = new XElement("LabPostResult", labPostResultList.Select(x => new XElement("row",
new XAttribute("PatientID", x.PatientID),
x.AnalyteName != null ? new XAttribute("AnalyteName", x.AnalyteName) : null,
new XAttribute("TestName", x.Loinc)
)));
This way the attribute won't be created for objects which don't have AnalyteName
.
score:1
You can write an extension method. It will much cleaner.
public static XElement ToXElement(this string content, XName name)
{
return content == null ? null : new XElement(name, content);
}
And call it as below.
public static void StoreResult(List<LabPostResult> labPostResultList)
{
var xml = new XElement("LabPostResult",
labPostResultList.Select(x => new XElement("row",
new XAttribute("PatientID", x.PatientID),
x.AnalyteName.ToXElement("AnalyteName"),
x.Loinc.ToXElement("TestName")
)));
}
Source: stackoverflow.com
Related Articles
- How to add XAttribute only if value is not null
- LINQ: adding where clause only when a value is not null
- SQL Trigger is trying to insert a null value but my C# code is passing int 300?
- Linq - Getting second value only if != null
- C# - Only set a the value of a variable if some other variable is not null
- Only include in where condition if value is not null in Linq
- How set XAttribute Value null
- LINQ to entity Error: "Unable to create a null constant value of type ''System.Int32[]". Only entity types, enumeration types
- The EntityKey property can only be set when the current value of the property is null
- Search Only if not Null value
- Unable to create a constant value of type Only primitive types or enumeration types are supported in this context
- FirstOrDefault: Default value other than null
- Value cannot be null. Parameter name: source
- linq, unable to create a constant value of type yyy. only primitive types or enumeration types are supported in this context
- LINQ syntax where string value is not null or empty
- How to resolve Value cannot be null. Parameter name: source in linq?
- Unable to create a constant value - only primitive types
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- The cast to value type 'Decimal' failed because the materialized value is null
- Return null instead default value in LINQ
- XAttribute default value if not existing on XElement
- How to assign empty string if the value is null in linq query?
- LINQ - Returning value of a property of an object that is not null
- Update XAttribute Value where XAttribute Name = X
- Linq GroupBy with each null value as a group
- Unable to create a constant value of type (type) Only primitive types ('such as Int32, String, and Guid') are supported in this context
- Null value cannot be assigned - LINQ query question
- Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context
- Linq GroupBy on multiple columns with potentials null value
- Compare only Date in nHibernate Linq on a DateTime value
- Dictionary<something, List<>> flatten (ungroup) to List of something and elements from List - C#
- Why XDocument.Load() and XmlDocument.LoadXml() can't load XML with comments contains more than two "--" symbols?
- How to move a DataTable row to the first position of its DataTable
- How can I Select an IEnumerable over 2 Tables using LINQ
- Modifying list with linq
- LINQ to SQL LEFT JOIN, GROUP BY, COUNT, MAX and subquery
- LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression."}
- Linq query to get multiple columns in c# Entity Framework
- LINQ - filter child collection
- How to get the sum of each Month?
- LINQ Aggregate Results in Entityframework Core
- Automatically create correct class of object when pulling data with LINQ
- Check a List of a class for a specific variable with LINQ
- Get the index of item in list based on value
- Grouping the details of the data into one, price field should be separated by semi-colon
- retrieve 5 last records from table by C#
- Difference betweeen FirstOrDefault() and FirstOrDefault<string>() in LINQ
- Sort List<Point> ListOfPoints that reside on a straight line in C#
- How can i combine two object fields in Object List into one DropDown in mvc
- LINQ JOIN + GROUP BY + SUM