开发者

Getting unique data from ISingleResult

I have a stored procedure that is returning data in this format:

HotelID | Price
---------------------
1 | 20
1 | 30
2 | 25
4 | 40
4 | 50

I'm getting the results like so:

ISingleResult<spResult> results = DataContext.sp();

I'd like to get a list of Hotels, based on the data returned from the stored procedure. Something like:

int[] uniqu开发者_JAVA技巧eHotelIds = GetUniqueHotelIdsFromResults(results);

List<Hotel> hotels = (from h in DataContext.Hotels
                      where uniqueHotelIds.Contains(h.HotelID)
                      select h).ToList();


I don't have much experience with ISingleResult, but could you do:

int[] uniqueHotelIds = results.Select(x => x.HotelID).Distinct();


You can also iterate through the SingleResult like this:

var myFancyResult;

ISingleResult<spResult> results = DataContext.sp();

foreach (var spResult in results)
{
    myFancyResult = spResultin.NameOfColumn;
    //Do something else with the data.
}

You can then name the result in the Stored Procedure like this, to give a clear picture:

SELECT ISNULL(@Result, 0) AS Result

ISNULL also ensures you don't have to deal with Nullables in C#.


In case anyone still needs the answer:

NOTE: ISingleResult, as the name implies, will return one result. In the code block below, it maps the resulting SP call to the "resultObject" class. The second line gives an example of the same code with parameters. You'd then want to check and make sure it's not null (depending on your SP).

ISingleResult<spResult> results = DataContext.sp();
var newObject = results.Select(r => new resultObject()).SingleOrDefault();
// or
var newObject = DataContext.sp().Select(r => new resultObject()).SingleOrDefault();
// with parameters
var newObject = DataContext.sp(id, count).Select(r => new resultObject(id, count)).SingleOrDefault();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜