Use split in LINQ

Source: Internet
Author: User

I am working on a project today. There is such a broken word IDs varchar (100)

When a list <string> ("16", "17") is passed in. In IDs, you need to find that IDs contains 16 or 17 broken words.

IDS stores such data. 16, 17

1, 2

3, 16

Okay... There are two methods. One is to use labmber and the other is to use SQL

Labmber:

Code
Parameterexpression parameter = expression. parameter (typeof (productinfo), "T ");
Expression prop = expression. Property (parameter, "categoryid ");
Expression exp = NULL;
Foreach (string s in productcategoryids)
{
Expression current = expression. Call (
Prop, typeof (string). getmethod ("startswith", new type [] {typeof (string)}), expression. Constant (S + ","));

Exp = (exp = NULL? Current: expression. Or (exp, current ));

Current = expression. Call (
Prop, typeof (string). getmethod ("contains"), expression. Constant ("," + S + ","));

Exp = (exp = NULL? Current: expression. Or (exp, current ));

Current = expression. Call (
Prop, typeof (string). getmethod ("endswith", new type [] {typeof (string)}), expression. Constant ("," + s ));

Exp = (exp = NULL? Current: expression. Or (exp, current ));

Current = expression. Equal (prop, expression. Constant (s ));

Exp = (exp = NULL? Current: expression. Or (exp, current ));
}

Expression <func <productinfo, bool> Lambda = expression. Lambda <func <productinfo, bool> (exp, parameter );
VaR productlist = Dc. productsinfo. Where (lambda );

 

In this way, you can. Productlist is an SQL statement .. But not executed. If you want to use multiple conditions or the like, you can directly set where to OK.

 

Type 2: Use stored procedures. First write a separator Method

 

Code
Set ansi_nulls on
Set quoted_identifier on
Go
Alter procedure [DBO]. [product_getproductbyid]
@ Strwhere nvarchar (500 ),
@ Pagesize Int = 100,
@ Pageindex Int = 1
As
Declare @ SQL nvarchar (4000) -- subject sentence
Set @ SQL ='
Begin
With temptbl as (select *,
Row_number () over (order by productcode ASC) as row from g_product where 1 = 1' + @ strwhere + ')
Select * From temptbl where row between (@ pageindex-1) * @ pagesize + 1 and (@ pageindex-1) * @ pagesize + @ pagesize
 
End
'

Exec sp_executesql @ SQL, n' @ pageindex int, @ pagesize int'
, @ Pageindex, @ pagesize

 

Write a paging record.

 

Code
Alter procedure [DBO]. [product_getproductbyid]
@ Strwhere nvarchar (500 ),
@ Pagesize Int = 100,
@ Pageindex Int = 1
As
Declare @ SQL nvarchar (4000) -- subject sentence
Set @ SQL ='
Begin
With temptbl as (select *,
Row_number () over (order by productcode ASC) as row from g_product where 1 = 1' + @ strwhere + ')
Select * From temptbl where row between (@ pageindex-1) * @ pagesize + 1 and (@ pageindex-1) * @ pagesize + @ pagesize
 
End
'

Exec sp_executesql @ SQL, n' @ pageindex int, @ pagesize int'
, @ Pageindex, @ pagesize

 

In this way, you can. Directly drag the stored procedure to dbml. Because directly dragging the above will become int type. You need to change the dbml method.

 

 

I wanted to send it to the home page. However, the homepage is too demanding. Besides, there is no advanced technology.

 

A summary of the project .. Haha... OK 88

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.