string[] words = { "hello", "wonderful", "linq", "beautiful", "world" };
or
string[] words = new string[] { "hello", "wonderful", "linq","beautiful", "world" };
var shortWords =
from word in words
where word.Length <= 5
select word;
foreach (var item in shortWords)
Console.WriteLine(item);
var s = words.Where(c => c.Length <= 5);
foreach (var item in s)
Console.WriteLine(item);
UnType Array
Object[] array = { "String", 12, true, 'a' };
foreach (var item in array)
Console.WriteLine(item);
var types = array.Select(item => item).Select(item => item.GetType().Name);
foreach (var item in types)
Console.WriteLine(item);
Multiple Object and LINQ -Filtering, sorting,comparing
class Book
{
public string Title { get; set; }
}
List<Book> books = new List<Book>()
{
new Book { Title="LINQ in Action" },
new Book { Title="LINQ for Fun" },
new Book { Title="Extreme LINQ" }
};
or
Book[] books = {
new Book { Title="LINQ in Action" },
new Book { Title="Action for Fun" },
new Book { Title="Extreme LINQ" }
};
var titles = books.Where(book => book.Title.Contains("Action")).Select(book => book.Title);
foreach (var item in titles)
Console.WriteLine(item);
Single object -check length,contain any string or find self attribute value
Book b = new Book();
b.Title = "pravin patil";
var s = b.Title.Length.Equals(("pravin patil").Length);
Console.WriteLine(s);
LINQ to XML
string usersName = "pravin";
cricket.Add(new XElement("Team",
new XAttribute("TeamName", "India"),
new XElement("batsman", "sachin",new XAttribute("Rank",1)),
new XElement("bowler", "ashwin")
)
);
XElement Team = new XElement("Team");
Team.Add( new XAttribute("TeamName", "AUS"),
new XElement("batsman", "asdas"),
new XElement("bowler", "asdasd")
);
cricket.Add(Team);
Console.WriteLine(cricket);
<book>
<title>abc</title>
<author>xxx</author>
</book>
</books>");
books.Element("book").Element("author").ReplaceNodes("foo");
Console.WriteLine(books);
@"<books>
<book>
<title>aaa</title>
<author>bbb</author>
</book>
</books>");
books.Element("book").ReplaceNodes(
new XElement("title", "Ajax in Action"),
new XElement("author", "no")
);
Console.WriteLine(books);
{
XElement xEle = XElement.Load(Server.MapPath("~\\images\\joblist\\FunctionalDesc.xml"));
var emp = xEle.Descendants("function").Where(func => (string)func.Attribute("functionid") == functionid);
var emps = from nm in xEle.Descendants("function")
where (string)nm.Attribute("functionid") == functionid
select nm;
foreach (var item in emp)
Response.Write(item);
}
or
string[] words = new string[] { "hello", "wonderful", "linq","beautiful", "world" };
var shortWords =
from word in words
where word.Length <= 5
select word;
foreach (var item in shortWords)
Console.WriteLine(item);
var s = words.Where(c => c.Length <= 5);
foreach (var item in s)
Console.WriteLine(item);
------------------------------------------------------
UnType Array
Object[] array = { "String", 12, true, 'a' };
foreach (var item in array)
Console.WriteLine(item);
var types = array.Select(item => item).Select(item => item.GetType().Name);
foreach (var item in types)
Console.WriteLine(item);
------------------------------------------------------
Multiple Object and LINQ -Filtering, sorting,comparing
class Book
{
public string Title { get; set; }
}
List<Book> books = new List<Book>()
{
new Book { Title="LINQ in Action" },
new Book { Title="LINQ for Fun" },
new Book { Title="Extreme LINQ" }
};
or
Book[] books = {
new Book { Title="LINQ in Action" },
new Book { Title="Action for Fun" },
new Book { Title="Extreme LINQ" }
};
var titles = books.Where(book => book.Title.Contains("Action")).Select(book => book.Title);
foreach (var item in titles)
Console.WriteLine(item);
Single object -check length,contain any string or find self attribute value
Book b = new Book();
b.Title = "pravin patil";
var s = b.Title.Length.Equals(("pravin patil").Length);
Console.WriteLine(s);
LINQ to XML
string usersName = "pravin";
XElement name = new XElement("name", usersName);
Console.WriteLine(name);
Output
<name>pravin
</name>
------------------------------------------------------
void Main()
{
XElement name = new XElement("method", GetUsersName());
Console.WriteLine(name);
}
private string GetUsersName() { return "method content can keep in xml"; }
Output
<method>method content can keep in xml</method>
------------------------------------------------------
XElement x= new XElement("book", new XAttribute("pubDate", "July 31, 2006"));
Console.WriteLine(x);
Result
<book pubDate="July 31, 2006" />
------------------------------------------------------
XElement x= new XElement("book", "hi how are you",new XAttribute("pubDate", "July 31, 2006"));
Console.WriteLine(x);
Result
<book pubDate="July 31, 2006">hi how are you</book>
------------------------------------------------------
XElement book = new XElement("book",new XAttribute("publicationDate", "October 2005"));
Console.WriteLine(book);
Output
<book publicationDate="October 2005" />
------------------------------------------------------XElement cricket = new XElement("cricket");
cricket.Add(new XElement("Team",
new XAttribute("TeamName", "India"),
new XElement("batsman", "sachin",new XAttribute("Rank",1)),
new XElement("bowler", "ashwin")
)
);
XElement Team = new XElement("Team");
Team.Add( new XAttribute("TeamName", "AUS"),
new XElement("batsman", "asdas"),
new XElement("bowler", "asdasd")
);
cricket.Add(Team);
Console.WriteLine(cricket);
<cricket>
<Team TeamName="India">
<batsman Rank="1">sachin</batsman>
<bowler>ashwin</bowler>
</Team>
<Team TeamName="AUS">
<batsman>asdas</batsman>
<bowler>asdasd</bowler>
</Team>
</cricket>
------------------------------------------------------
cricket.Add(new XElement("Team",
new XAttribute("TeamName", "India"),
new XElement("batsman", "sachin",new XAttribute("Rank",1)),
new XElement("bowler", "ashwin")
),
new XElement("Team",
new XAttribute("TeamName", "AUS"),
new XElement("batsman", "sdf",new XAttribute("Rank",1)),
new XElement("bowler", "sdf")
)
);
Console.WriteLine(cricket);
<cricket>
<Team TeamName="India">
<batsman Rank="1">sachin</batsman>
<bowler>ashwin</bowler>
</Team>
<Team TeamName="AUS">
<batsman Rank="1">sdf</batsman>
<bowler>sdf</bowler>
</Team>
</cricket>
------------------------------------------------------
XElement book = new XElement("book");
book.Add(new XElement("author", "Don Box"));
book.Add(new XElement("title", "Essential .NET"));
XElement bag = new XElement("bag");
bag.Add(book);
Console.WriteLine(bag);
<bag>
<book>
<author>Don Box</author>
<title>Essential .NET</title>
</book>
</bag>
------------------------------------------------------
XElement books = XElement.Parse(@"<books>
<book>
<title>abc</title>
<author>xxx</author>
</book>
</books>");
books.Element("book").Element("author").ReplaceNodes("foo");
Console.WriteLine(books);
<books>
<book>
<title>abc</title>
<author>foo</author>
</book>
</books>
------------------------------------------------------
XElement books = XElement.Parse(
@"<books>
<book>
<title>LINQ in Action</title>
<author>Steve Eichert</author>
</book>
</books>");
books.Element("book").SetElementValue("author", "foo");
Console.WriteLine(books);
<books>
<book>
<title>LINQ in Action</title>
<author>foo</author>
</book>
</books>
------------------------------------------------------
XElement books = XElement.Parse(@"<books>
<book>
<title>aaa</title>
<author>bbb</author>
</book>
</books>");
books.Element("book").ReplaceNodes(
new XElement("title", "Ajax in Action"),
new XElement("author", "no")
);
Console.WriteLine(books);
<books>
<book>
<title>Ajax in Action</title>
<author>no</author>
</book>
</books>
------------------------------------------------------
public void FetchFunctionInfo(string functionid){
XElement xEle = XElement.Load(Server.MapPath("~\\images\\joblist\\FunctionalDesc.xml"));
var emp = xEle.Descendants("function").Where(func => (string)func.Attribute("functionid") == functionid);
var emps = from nm in xEle.Descendants("function")
where (string)nm.Attribute("functionid") == functionid
select nm;
foreach (var item in emp)
Response.Write(item);
}
No comments:
Post a Comment