Hi folks.
i've got a LinqToSql query with a custom extension method at the end. this extension method is erroring when I try to linq2sql tries to generate the sql statement.
Error:
Method 'System.Collections.Generic.IList
1[System.String] ToListIfNotNullOrEmpty[String](System.Collections.Generic.IEnumerable
1[System.String])' has no supported translation to SQL.
Extension Method:
public static IList<T> ToListIfNotNullOrEmpty<T>(this IEnumerable<T> value)
{
return value.IsNullOrEmpty()
? null
: (value is IList<T> ? value as IList<T> : new List<T>(value));
}
Sample Linq to sql code:
public IQueryable<Models.Post> GetPosts()
{
var dataLoadOptions = new DataLoadOptions();
dataLoadOptions.LoadWith<Post>(x => x.PostTags);
dataLoadOptions.LoadWith<PostTag>(x => x.Tag);
_sqlDatabase.LoadOptions = dataLoadOptions;
return from p in _sqlDatabase.Posts
select new Models.Post
{
PostId = p.PostId,
CommentList = (from c in p.Comments
select new Models.Comment
{
PostId = c.PostId,
< ... stuff snipped ... >
}).ToListIfNotNullOrEmpty(),
< ... more stuff snipped ... >
TagList = (from t in p.PostTags
select t.Tag.Description).ToListIfNotNullOrEmpty(),
};
}
Normally, I would just have a ToList() method at the end, but i want to return a null object if the list is empty .. hence my extension method.
Can anyone suggest how I should fix this to make it work?