开发者

validate search result using Linq

I have a search result from the database,

int searchTerm = "xx"

var result = from orderLines in context.OrderLineSearch(searchTerm)
                                    sele开发者_开发技巧ct new
                                    {
                                        OrderNumber = orders.OrderNumber,
                                        OrderLineId = orders.OrderLineId
                                    }; 

I need to validate whether result contains many orders. That is I need to check whether multiple orders are returned or not.

I did the validation by storing the first order number in a variable and compared whether all the other rows contains only this order number. See below

string orderNumber = result.First().OrderNumber;

bool isValid = result.Where(x => x.OrderNumber != orderNumber).Count() == 0;

I want to know the best way to validate using LINQ? Can any one help me?

Thanks in advance.


May be you should to try group results by OrderNumber and then calculate Count?

var result = from orderLines in context.OrderLineSearch(searchTerm)
             group orderLines by orderLines.OrderNumber into g   
             select g.Key     
bool hasElements = result.Any();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜