コード スニペット: 検索の実装
最終更新日: 2010年4月19日
適用対象: SharePoint Server 2010
この記事の内容
.NET Connectivity Assembly での例
ASP.NET Web サービスでの例
WCF サービスでの例
その他のコード例
以下のコード例では, .NET Connectivity Assembly と Web サービスに、Finder メソッド インスタンスを実装する方法を示します。
.NET Connectivity Assembly での例
public Customer[] GetCustomers(String name, int? limit)
{
if (name != "")
{
List<Customer> customersWithName = new List<Customer>();
foreach (Customer customer in customers)
{
//$Name$
if (name.StartsWith("$"))
{
if (name.EndsWith("$"))
{
if (customer.Name.Contains(name.Substring(1,
(name.Length - 2))))
{
customersWithName.Add(customer);
}
}
else
{
if (customer.Name.EndsWith(name.Substring(1,
(name.Length - 1))))
{
customersWithName.Add(customer);
}
}
}
else if (name.EndsWith("$"))
{
if (customer.Name.StartsWith(name.Substring(0,
(name.Length - 1))))
{
customersWithName.Add(customer);
}
}
//Name
else if (customer.Name == name)
{
customersWithName.Add(customer);
}
}
if (limit < 1)
return customersWithName.FindAll(
cust => cust.IsDeleted == false).ToArray();
else
{
int limitvalue = Convert.ToInt32(limit);
List<Customer> limitedcustomers = new List<Customer>();
for (int i = 0; i < limitvalue & i < customersWithName.Count;
i++)
limitedcustomers.Add(customersWithName[i]);
return limitedcustomers.FindAll(
cust => cust.IsDeleted == false).ToArray();
}
}
else
{
if (limit < 1)
return customers.FindAll(
cust => cust.IsDeleted == false).ToArray();
else
{
int limitvalue = Convert.ToInt32(limit);
List<Customer> limitedcustomers = new List<Customer>();
for (int i = 0; i < limitvalue & i < customers.Count; i++)
limitedcustomers.Add(customers[i]);
return limitedcustomers.FindAll(
cust => cust.IsDeleted == false).ToArray();
}
}
}
ASP.NET Web サービスでの例
[WebMethod]
public Customer[] GetCustomers(String name, int? limit)
{
if (name != "")
{
List<Customer> customersWithName = new List<Customer>();
foreach (Customer customer in customers)
{
//$Name$
if (name.StartsWith("$"))
{
if (name.EndsWith("$"))
{
if (customer.Name.Contains(
name.Substring(1, (name.Length - 2))))
{
customersWithName.Add(customer);
}
}
else
{
if (customer.Name.EndsWith(
name.Substring(1, (name.Length - 1))))
{
customersWithName.Add(customer);
}
}
}
else if (name.EndsWith("$"))
{
if (customer.Name.StartsWith(
name.Substring(0, (name.Length - 1))))
{
customersWithName.Add(customer);
}
}
//Name
else if (customer.Name == name)
{
customersWithName.Add(customer);
}
}
if (limit < 1)
return customersWithName.FindAll(
cust => cust.IsDeleted == false).ToArray();
else
{
int limitvalue = Convert.ToInt32(limit);
List<Customer> limitedcustomers = new List<Customer>();
for (int i = 0; i < limitvalue & i < customersWithName.Count;
i++)
limitedcustomers.Add(customersWithName[i]);
return limitedcustomers.FindAll(
cust => cust.IsDeleted == false).ToArray();
}
}
else
{
if (limit <1)
return customers.FindAll(
cust => cust.IsDeleted == false).ToArray();
else
{
int limitvalue = Convert.ToInt32(limit);
List<Customer> limitedcustomers = new List<Customer>();
for (int i = 0; i < limitvalue & i < customers.Count;
i++)
limitedcustomers.Add(customers[i]);
return limitedcustomers.FindAll(
cust => cust.IsDeleted == false).ToArray();
}
}
}
WCF サービスでの例
以下のコードは、サービス コントラクト インターフェイスでの操作定義を示します。
[OperationContract]
Customer[] GetCustomers(string name, int? limit);
以下の例は、メソッド インスタンスの実装を示します。
public Customer[] GetCustomers(String name, int? limit)
{
if (name != "")
{
List<Customer> customersWithName = new List<Customer>();
foreach (Customer customer in customers)
{
//$Name$
if (name.StartsWith("$"))
{
if (name.EndsWith("$"))
{
if (customer.Name.Contains(
name.Substring(1, (name.Length - 2))))
{
customersWithName.Add(customer);
}
}
else
{
if (customer.Name.EndsWith(
name.Substring(1, (name.Length - 1))))
{
customersWithName.Add(customer);
}
}
}
else if (name.EndsWith("$"))
{
if (customer.Name.StartsWith(
name.Substring(0, (name.Length - 1))))
{
customersWithName.Add(customer);
}
}
//Name
else if (customer.Name == name)
{
customersWithName.Add(customer);
}
}
if (limit < 1)
return customersWithName.FindAll(
cust => cust.IsDeleted == false).ToArray();
else
{
int limitvalue = Convert.ToInt32(limit);
List<Customer> limitedcustomers = new List<Customer>();
for (int i = 0; i < limitvalue & i < customersWithName.Count;
i++)
limitedcustomers.Add(customersWithName[i]);
return limitedcustomers.FindAll(
cust => cust.IsDeleted == false).ToArray();
}
}
else
{
if (limit < 1)
return customers.FindAll(
cust => cust.IsDeleted == false).ToArray();
else
{
int limitvalue = Convert.ToInt32(limit);
List<Customer> limitedcustomers = new List<Customer>();
for (int i = 0; i < limitvalue & i < customers.Count; i++)
limitedcustomers.Add(customers[i]);
return limitedcustomers.FindAll(
cust => cust.IsDeleted == false).ToArray();
}
}
}
その他のコード例
外部システム - データベース/SQL Server
たとえば、Microsoft SQL Server データベースの Contact エンティティに対しては、Finder メソッドは以下のようになります。
public static IEnumerable<Contact> ReadList(int itemLimit) { const string ServerName = "MySQLServerName"; AdventureWorksDataContext dataContext = new AdventureWorksDataContext ("Data Source=" + ServerName + ";" + "Initial Catalog=AdventureWorks;Integrated Security=True"); IEnumerable<Contact> Contacts = from contacts in dataContext.Contacts.Take(itemLimit) select contacts; return Contacts; }
以下に、フィルター処理可能なパラメーターを持つ Finder メソッドのより複雑な例を示します。
SELECT ProductID, Name, ProductNumber, ListPrice FROM Product WHERE (ProductID >= @MinProductID) AND (ProductID <= @MaxProductID) AND (Name LIKE @Name) AND ProductNumber LIKE @ProductNumber)
外部システム - フラット ファイル
public static IEnumerable<FlatFileEntity> ReadList() { List<FlatFileEntity> flatFileEntityList = new List<FlatFileEntity>(); TextReader textReader = new StreamReader(@"c:\data\flat-file-data-source.txt"); string row; while ((row = textReader.ReadLine()) != null) { FlatFileEntity flatFileEntity = new FlatFileEntity(); string[] entityData = row.Split(','); flatFileEntity.ID = entityData[0]; flatFileEntity.Company = entityData[1]; flatFileEntity.FirstName = entityData[2]; flatFileEntity.LastName = entityData[3]; flatFileEntity.Address = entityData[4]; flatFileEntity.City = entityData[5]; flatFileEntity.State = entityData[6]; flatFileEntity.ZipCode = entityData[7]; flatFileEntity.Phone = entityData[8]; flatFileEntity.LastUpdated = DateTime.Parse(entityData[9]); flatFileEntityList.Add(flatFileEntity); } textReader.Close(); return flatFileEntityList.toArray(); }
ページング サンプル
public static IEnumerable<Document> ReadFolder( string parentFolderID, int pageNumber) { //Required to allow SQL CE to work in an ASP.NET environment AppDomain.CurrentDomain.SetData( "SQLServerCompactEditionUnderWebHosting", true); DMSEntities ctx = new DMSEntities(connString); int tid = int.Parse(parentFolderID); string username = GetCurrentDMSUsername(); /* SQL CE does not support paging so we must return all results, create an anonymous type, and then take the page. In production, you would do this all in a single LINQ statement. */ var a = ((from d in ctx.Documents where (d.ParentId == tid) select new { d.Name, d.Id, d.ParentId, d.IsFolder }). OrderBy(d => !d.IsFolder)).ToList(); //Get authorized documents by page. var p = a.Where(c => IsAuthorized(username, c.Id) == true). Skip((pageNumber - 1) * PageSize).Take(PageSize); List<Document> docs = new List<Document>(); foreach (var i in p) { Document doc = new Document(); doc.ID = i.Id.ToString(); doc.Name = i.Name; doc.ParentID = i.ParentId.ToString(); if (i.IsFolder) doc.Type = "Folder"; else doc.Type = "Document"; doc.Version = "Published"; docs.Add(doc); } return docs; }