No path is too difficult. No destination is too far !
Posts tagged C#
How to convert from IEnumerable to EntitySet to get Entity back in action
Nov 27th
One way of achieving is to use what most google search results find on web i.e.
public static EntitySet<T> ToEntitySet<T> (this IEnumerable<T> source) where T : class
{
var es = new EntitySet<T> ();
es.AddRange (source);
return es;
}
Then you could write your query as follows:
var questions = from q in e.Element(“Questions”).Elements(“Question”)
select new Question
{
Text = q.Attribute(“title”).Value,
Point = (decimal) q.Attribute(“point”),
Choices = (
from c in q.Elements(“Choices”).Elements(“Choice”)
select new Choice
{
Text = c.Attribute(“title”).Value,
Correct = (bool) c.Attribute(“isCorrect”)
}
).ToEntitySet();
};
But to achieve this, one may need to add reference to System.Data.Linq and even then there are sometimes when you don’t find the solution, in cases where only one Entity is required.
A very simple and effective trick for a work around of this problem is to use the following :
var FirstQuestion = Questions.Where(q=>q.ID==someIDParameter).ToList().FirstorDefault();
Now use this and do the work peacefully.
Create Word Document with Hyperlinks in it with C# code
Feb 18th
First of all you need to add reference to COM library in project.
Add reference to Microsoft word 11.0 word library under COM reference tab Add this in the code behind “using” section
using Microsoft.Office.Interop.Word;
private void CreateWordDocument()
{
// create annonymous object to be referenced throughout
object oMissingReference = System.Reflection.Missing.Value;
object Visible=true;
// define arbitrary starting point for defining range object.
object oStartRange = 0;
object oEndRange = 0;
// create instance of application class
ApplicationClass WordApp = new ApplicationClass();
// create document instance
Document docObject = WordApp.Documents.Add(ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference );
// select a range to add some text/image there
Range rng=docObject.Range(ref oStartRange,ref oMissingReference );
rng.Font.Name=”Arial”;
rng.InsertAfter(”Ashish Sehajpal”);
// create Hyperlink object and assign address, screen tip to it
Object address = @”http://www.sehajpal.com”;
Object screenTip = “Welcome to sehajpal.com”;
// adding Hyperlink to the text “Ashish Sehajpal“
rng.Hyperlinks.Add(rng, ref address, ref oMissingReference , ref screenTip, ref oMissingReference , ref oMissingReference );
// give file name to it so as to save the document
object filename = @”C:\createdbycsharp.doc”;
docObject.SaveAs(ref filename, ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference ,ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference );
WordApp.Visible=true;
}
Calling constructor inside constructor in C#
Feb 16th
To call one C# constructor from another, before the body of the constructor, use either:
: base (parameters)
to call a constructor in the base class; or:
: this (parameters)
to call a constructor in this class.
The following examples illustrate how to call one constructor from another.
public class myPopUp
{
public myPopUp(int width, int height, myPopUp parentPopUp) : this (width, height)
{
this.ParentPopUp = parentPopUp;
}
}