{
var predicate = PredicateBuilder.False
foreach (string keyword in keywords)
{
string temp = keyword;
predicate = predicate.Or (p => p.Description.Contains (temp));
}
return dataContext.Products.Where (predicate);
}
see C# in a Nutshell
So have you ever had a great idea? I had this great idea while talking with another developer at work today. What if you could write and compile code online (in the cloud) rather than in an IDE (integrated development environment)? using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DripCode.Text
{
public static class ExtensionMethods
{
public static Boolean ContainsAt(this string line, string template, int location)
{
return line.Contains(template) && line.Length >= template.Length + location && line.Substring(location, template.Length) == template;
}
}
}
string Test = "hello world!"
Console.WriteLine(Test.ContainsAt("hello",0).ToString());
Console.WriteLine(Test.ContainsAt("world",6).ToString());
Similarly you can do a IN like filter in LINQ:
SELECT * FROM MemberInfomation WHERE @samAccount IN ('adf','asfd','wqewe')
Got help from here.
public ListGetInformation(String[] samAccounts)
{
SomeDataContextdc = new SomeDataContext();
return dc.MemberInfomation.Where(
o => samAccounts.Contains(o.samaccount)
).ToList();
}
#region Throwing Exception Tests
private void MethodToTest()
{
throw new ArgumentException("blah");
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Test_ExceptionThrown_Attribute()
{
try
{
MethodToTest();
}
catch (Exception ex)
{
if (ex.Message == "blah") //test message value
throw new ArgumentException(ex.Message);
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Test_ExceptionThrown_Attribute_Function()
{
VerifyExceptionThrown_Throw(() => MethodToTest(), "blah"); // simple, single line, and generic.
}
// Method accepts an action delegate, a generic Exception type, and an exception message to check
public static void VerifyExceptionThrown_Throw(Action action, String exMessage)
where TException : Exception
{
// Verify arguments - action can't be null
if (action == null)
throw new ArgumentException("Action to test cannot be null!");
// Verify arguments - TException cannot be of type Exception
if (typeof(TException).Equals(typeof(Exception)))
throw new ArgumentException("TException type cannot be of type Exception");
// Actually call it
try
{
action();
}
catch (TException ex)
{
if (ex.Message == exMessage)
{
throw new ArgumentException("good job"); //we must rethrow an exception because of ExpectedException attribute
}
}
// If it gets here, fail the test
Assert.Fail("Action does not throw the exception expected");
}
//
[TestMethod]
public void Test_ExceptionThrown_NoAttribute()
{
try
{
MethodToTest();
Assert.Fail("Action does not throw the exception expected");
}
catch (ArgumentException ex)//specifing the type of exception to test for
{
if (ex.Message != "blah") //test message value
Assert.Fail("Action does not throw the exception expected"); //failed
}
}
[TestMethod]
public void Test_ExceptionThrown_NoAttribute_Function()
{
VerifyExceptionThrown_NoThrow(() => MethodToTest(), "blah"); //simple, generic, message parameter is optional... and the TestMethod doesn't need the attribute
}
// Method accepts an action delegate, a generic Exception type, and an exception message to check (optional)
public static void VerifyExceptionThrown_NoThrow(Action action, String exMessage)
where TException : Exception
{
// Verify arguments - action can't be null
if (action == null)
throw new ArgumentException("Action to test cannot be null!");
// Verify arguments - TException cannot be of type Exception
if (typeof(TException).Equals(typeof(Exception)))
throw new ArgumentException("TException type cannot be of type Exception");
// Actually call it
try
{
action();
}
catch (TException ex)
{
if (exMessage == null) //don't want to consider the message
{
return;
}
else //otherwise consider the message
{
if (ex.Message == exMessage)
{
return;
}
}
}
// If it gets here, fail the test
Assert.Fail("Action does not throw the exception expected");
}
#endregion
using Google.GData.Client;
using Google.GData.GoogleBase;
public static class GoogleBaseFacade
{
private static GBaseService service;
private static void Connect()
{
if (service == null)
{
service = new GBaseService("TEST - Homes for Sale", <google key>);
service.setUserCredentials(<login>, <password>);
service.NewAtomEntry += new FeedParserEventHandler(service_NewAtomEntry);
}
}
static void service_NewAtomEntry(object sender, FeedParserEventArgs e)
{
if (e.Feed != null && e.Feed.Entries != null && e.Feed.Entries.Count > 0)
{
foreach (AtomEntry entry in e.Feed.Entries)
{
if (entry.BatchData != null)
{
Console.WriteLine("New Entry STATUS CODE: " + entry.BatchData.Status.Code.ToString());
}
else if (entry.Id != null && entry.Id.Uri != null)
{
Console.WriteLine("New Entry URI: " + entry.Id.Uri);
}
else
{
Console.WriteLine("New Entry ???");
}
}
}
else
{
Console.WriteLine("No Feed or Entries in feed. Possible single entry");
}
}
public static void AddAHome()
{
Connect();
//create an entry
GBaseEntry entry = new GBaseEntry();
AtomPerson author = new AtomPerson(AtomPersonType.Author,"James Smith");
author.Email="jamessmith@example.com";
entry.Authors.Add(author);
entry.GBaseAttributes.AddTextAttribute("target_country", "AU");
entry.GBaseAttributes.ItemType = "Housing";
entry.GBaseAttributes.AddNumberAttribute("bathrooms", 2);
entry.GBaseAttributes.AddNumberAttribute("bedrooms", 3);
entry.GBaseAttributes.AddTextAttribute("description", "A fantastic underground house.");
entry.Links.Add(new AtomLink("http://www.velociraptorz.org"));
entry.GBaseAttributes.AddTextAttribute("listing_type", "residential for sale");
entry.GBaseAttributes.AddNumberAttribute("price", 1200000);
entry.GBaseAttributes.AddTextAttribute("property_type", "house");
entry.GBaseAttributes.Location = "1 Market St, Sydney, NSW, 2000";
entry.Title.Text = "A safe haven, underground.";
entry.BatchData = new GDataBatchEntryData("blahblah", GDataBatchOperationType.insert);
entry.BatchData.Id = "batch1234";
AtomFeed batchFeed = new AtomFeed(GBaseUriFactory.Default.ItemsFeedUri, service);
batchFeed.Entries.Add(entry);
GBaseFeed returnFeed = service.Batch(batchFeed, GBaseUriFactory.Default.ItemsFeedBatchUri) as GBaseFeed;
}
}
//put this in a static class, singleton, abstract class, or whatever fits your need
private SortedList _CriteriaItem;
//get data from database and iterate over the dataset to populate the list
public void Add(object Key, object Value)
{
_CriteriaItem.Add(Key, Value);
}
public String GetDescriptionByCode(String key)
{
if (_Item.ContainsKey(key))
{
return _Item[key].ToString();
}
return "";
}
public String GetCodeByDescriptionBy(String description)
{
if (_Item.ContainsValue(description))
{
foreach (String key in _Item.Keys)
{
if (_Item[key].ToString() == description)
{
return key.ToString();
}
}
}
return "";
}