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 Query
- Need to join two strings if both contain values, or return one value if first is NULL
- LINQ returns List<{int,double}> two values after .selected but I need List<int> with one value only
- linq query to join two tables and get the count from one table values from the other
- C# LINQ Select objects with the same value of one property join values of other
- Intersect two lists and return the similarity with the preserved order of the original first string value
- C# compare two Lists by one Property and change value of first List
- How to Select two values from one value
- Join using LINQ between two tables, return records from only one of them
- Linq query to join two tables and return object from first table - PagedList used
- Return true if two lists contain any common values
- Need to get all records from one table, that match values I return from another table
- How is it possible that selecting first after GroupBy on a DataTable does not return one value for every group?
- join two lists and check for the matched property value and return the required data
- Linq join on multpile columns with a where not equals condition and need to return columns from both tables
- Compare two strings and check the changed value in a contain range
- How to return all values of a column as null except one column value in LINQ?
- Join two tables, select column based on value in first table
- LINQ Inner Join - Return one object from anonymous which is combined from Both Tables
- LINQ to SQL value BETWEEN two double values
- Left join on two Lists and maintain one property from the right with Linq
- How do you return a default value if a LINQ to entities query returns no values
- Linq query - find strings based upon first letter b/w two ranges
- SQL: Inner Join return one row based on criteria
- How can I make my Linq select return values if the value selected is null?
- How can I combine this code into one or two LINQ queries?
- Difference between the returned values of two selector functions in this code
- Linq join() - Join two entities and select one
- EF Code First comparing null values generates strange query
- Lambda expression to return one result for each distinct value in list
- How should I return two lists of objects from one Controller Action?
More Query from same tag
- using Any() with IQueryable List takes long time when executing?
- How can I get a list of DirectoryInfo where the name of the directories contains a string stored in a List<string>?
- Apply MemberExpression to an object to retrieve Property value
- Why does a null string concatenated to a non null string evaluate to null?
- GroupBy doesnt work on Readonly Collection
- How to filter specific columns from a child table Entity Framework
- How to create bill with multiple items and quantities, remove item, merge bill, clone bill
- Can XML with optional elements be used with LINQ and Anonymous Types?
- LINQ Order By to select lowest Index
- XNamespace url is written in XAttribute when I create my XML
- LINQ Query question
- LINQ multiple keyword search to PagedList
- Initialize a Linq to Sql object via business logic
- Get nested UserRoles from User table
- Sql to Linq like syntax
- Expression Tree Binary Expression for an 'In' operation
- List and create in same view in asp.net mvc
- Caching in Entity Framework Extended and Expression
- LINQ and sql query with left join and select max subquery
- Using LINQ in cross-join query of a datatable in C#
- LINQ vs. nHibernate
- How to merge two lists of different types into one list of new type that contains both types?
- Sum(with Coalesce) with Group By in linq (for EF6)
- Join two lists based on a condition other than equals
- Still get repeated strings when using 'Distinct()'
- How can i cast IQueryable<> query to IQueryable<obj.getType()>?
- Load the result of LINQ XML file to datagrid
- Dynamic Linq to OrderBy Object Nested in IEnumerable
- C# Anyway to Aggregate from IQueryable (Not AsEnumerable)
- LINQ to find series of consecutive numbers