Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Split in a Linq-To-SQL Query?

I have a database table that contains an nvarchar column like this:

1|12.6|18|19

I have a Business Object that has a Decimal[] property.

My LINQ Query looks like this:

var temp = from r in db.SomeTable select new BusinessObject {
    // Other BusinessObject Properties snipped as they are straight 1:1
    MeterValues = r.MeterValues.Split('|').Select(Decimal.Parse).ToArray()
};
var result = temp.ToArray();

This throws an NotSupportedException: Method 'System.String[] Split(Char[])' has no supported translation to SQL.

That kinda sucks :) Is there any way I can do this without having to add a string property to the business object or selecting an anonymous type and then iterating through it?

My current "solution" is:

var temp = from r in db.SomeTable select new {
    mv = r.MeterValues,
    bo = new BusinessObject { // all the other fields }
};
var result = new List<BusinessObject>();
foreach(var t in temp) {
    var bo = t.bo;
    bo.MeterValues = t.mv.Split('|').Select(Decimal.Parse).ToArray();
    result.Add(bo);
}
return result.ToArray(); // The Method returns BusinessObject[]

That's kinda ugly though, with that temporary list.

I've tried adding a let mv = r.MeterValues.Split('|').Select(Decimal.Parse).ToArray() but that essentially leads to the same NotSupportedException.

This is .net 3.5SP1 if that matters.

like image 218
Michael Stum Avatar asked Nov 29 '10 00:11

Michael Stum


People also ask

How to split string in LINQ query?

LINQ to objects example In the example method Split() divides string into array of words (strings) using space as separator. Array in . NET is IEnumerable and extension method Where can be called on it.

How split a string in SQL query?

The STRING_SPLIT(string, separator) function in SQL Server splits the string in the first argument by the separator in the second argument. To split a sentence into words, specify the sentence as the first argument of the STRING_SPLIT() function and ' ' as the second argument. FROM STRING_SPLIT( 'An example sentence.

What is the function of the split () in LINQ?

LINQ queries can be combined with traditional string functions and regular expressions. For example, you can use the String. Split or Regex. Split method to create an array of strings that you can then query or modify by using LINQ.

Is it good to use LINQ in C#?

It is probably the most important thing you want to learn in C#. TLDR; This article describes the usage of the library Linq that is part of the . NET framework. Linq enables you to query all sorts of data sources with a SQL like syntax.


2 Answers

You need to force the select clause to run on the client by calling .AsEnumerable() first:

var result = db.SomeTable.AsEnumerable().Select(r => new BusinessObject {
    ...
    MeterValues = r.MeterValues.Split('|').Select(Decimal.Parse).ToArray()
}).ToList();
like image 79
SLaks Avatar answered Nov 15 '22 17:11

SLaks


You can't use split, but in this scenario you can do the following:

// Database value is 1|12.6|18|19

string valueToFind = "19";

var temp = from r in db.SomeTable.Where(r => ("|" + r.MeterValues + "|").Contains("|" + valueToFind + "|"));

This code adds outer pipes (|) to the database value on the fly inside the query so you can do start, middle, and end value matches on the string.

For example, the above code looks for "|19|" inside "|1|12.6|18|19|", which is found and valid. This will work for any other valueToFind.

like image 35
mhapps Avatar answered Nov 15 '22 19:11

mhapps