score:3
try
var recent = from p in dc.Properties
orderby p.modtime descending
where p.status == "Current"
select new
{
rsub = (p.subNumber).ToString(),
rnumber = (p.streetNumber).ToString(),
rnum = string.IsNullOrEmpty((p.subNumber).ToString()) ? (p.streetNumber).ToString() : (p.subNumber).ToString() + "/" + (p.streetNumber).ToString(),
rstreet = p.street,
rsuburb = p.suburb,
rurl = p.propertyURL,
};
score:0
Just a ternary conditional aka ?: operator in the "select" should do:
select new
{
house = p.subNumber != null
? p.subNumber + "/" + p.streetNumber
: p.streetNumber;
...
};
This makes the assumption street number is always there (or it might result in "xxx/" or null
). It also assumes that sub is null
(not just empty) if truly not present.
If it starts to get "too complicated", consider the following (which has slightly different rules than above, those are left to be figured out):
select new
{
house = PrettyHouseNumber(p.subNumber, p.streetNumber),
...
};
string PrettyHouseNumber(string sub, string street) {
// ?: could also be used here as well, but since invoking the method
// can be used as an expression itself, breaking it up like this also
// allows the use of other constructs
if (!string.IsNullOrEmpty(sub)) {
return sub + "/" + street;
} else {
return "" + street; // NULL will go to "", if it can even ever come up
}
}
Which should show how any expression, including a method call, can be used there -- pass it some data and get some data back :) While there are limits with expression trees and which ones can be efficiently turned into SQL, since this is just processing data already returned then there is nothing to worry about here.
Happy coding.
score:0
On the table level you could create a computed column; these are not stored - the value is 'created' when the column is queried.
CREATE TABLE [Customer]
(
[subNumber] NVARCHAR(256),
[streetNumber] NVARCHAR(256),
[fullAddress] AS (CASE
WHEN [subNumber] IS NULL THEN [streetNumber]
ELSE [subNumber] + N' ' + [streetNumber]
END)
);
GO
Or you can add it the table:
ALTER TABLE [Customer]
ADD COLUMN [fullAddress]
AS (CASE
WHEN [subNumber] IS NULL THEN streetNumber
ELSE [subNumber] + N' ' + streetNumber
END);
GO
Now the value will be directly-accessible from your EF model.
Source: stackoverflow.com
Related Articles
- Need to join two strings if both contain values, or return one value if first is NULL
- Linq join on multpile columns with a where not equals condition and need to return columns from both tables
- How do you return a default value if a LINQ to entities query returns no values
- C# LINQ Select objects with the same value of one property join values of other
- How can I make my Linq select return values if the value selected is null?
- Intersect two lists and return the similarity with the preserved order of the original first string value
- EF Code First comparing null values generates strange query
- How to Create a Run-Time Computed (NotMapped) Value in Entity Framework Code First
- LINQ Inner Join - Return From Both Tables
- How to do Linq LIKE or Contains when both values have strings added to them
- Can I receive both the return value and resultset from a procedure using Linq to sql?
- Linq replace values until first X value
- Need to return an object with the lowest property value from List of objects
- return the first longest string consisting of n consecutive strings taken in the array in c#
- Using only Linq or Lambda, how do I combine both pieces of code to return List<Entity> e?
- Comparing a list of CSV values in Linq to match those from second list with any value in first list
- How do I do a right join and return only null values in EntityFramework?
- LINQ function to return list but compiler says function doesn't return a value on all code path
- "Subquery returned more than 1 value. This is not permitted." Need to return a set of values
- Linq query to Join tables if value not in first table fetch from second
- Linq query to join two tables and return object from first table - PagedList used
- How can I return a single value integer from Linq from an array of objects that have other nested values
- Error 1 'Form1.CountWordsInstances(string, string)': not all code paths return a value
- Return value of stored procedure is correct but column values are all NULL
- LINQ check list of objects contain a value AND not some other values
- Return true if two lists contain any common values
- LINQ returns List<{int,double}> two values after .selected but I need List<int> with one value only
- Just need to return distinct values from Excel using EPPlus
- List or Array of String Contain specific word in Html Source Code
- How to query a list and return values where ID = any value in an array
- C#'s LINQ and .NET Framework, which one depends on the other?
- convert this linq expression with loop(s)
- Extra Columns While Converting Linq Result into Data Table
- LINQ/C# - Making a DTO from a collection?
- Is there any way to get a JSON object as a list of view models and update records in database with single AJAX call?
- Creating a plan repository, but using keyword in constructor body
- How to query a date range from a varchar column using LINQ
- How does one make .Include work with a segmented query?
- SQL & LINQ - Distinct by 2 columns and "reversed values"
- How do I work with an XML tag within a string?
- How can I provide default expression in the { } of a lambda expression, while still allowing it to be added to?
- Linq to xml: generic approach
- What are the benefits of a Deferred Execution in LINQ?
- Get Elements from one XElement that are not in another XElement
- how to get specific fields in asp.net core entity in one to many relationship classes
- Update one to many table Entity Framework
- Entity Framework GroupJoin different result vs join with group
- Property setter trims a value assigned from LINQ, but isn't displayed as such in WPF datagrid
- Select only defined column in LINQ
- Passing type members generically to operate on generic collection