Linq to CSV select by column - linq

If I have the following (sample) text file;
year,2008,2009,2010
income,1000,1500,2000
dividends,100,200,300
net profit,1100,1700,2300
expenses,500,600,500
profit,600,1100,1800
Is there a way in Linq that I can select the expenses for 2010 only?
So far I have the following which gets me all the data;
var data = File.ReadAllLines(fileName)
.Select(
l => {
var split = l.CsvSplit();
return split;
}
);
foreach (var item in data)
Console.WriteLine("{0}: ${1}", item[0], item[1]);

If you know it's always the 3rd value column, then
// the expenses row
var query = data.Single(d => d[0] == "expenses");
// the third column
return query[3];
and if you don't, then
var columnNumber = Array.IndexOf(data.First(), "2010");
return query[columnNumber];

See LINQtoCSV, its a library that does all this for you. I've used it, and it works like a charm.
http://www.codeproject.com/KB/linq/LINQtoCSV.aspx

Related

Trim whitespace from DataTable cells with Linq

This piece of code works to trim all spaces in each datacell of each datarow.
How can I get this code:
var dataRows = dataTable.AsEnumerable();
foreach (var row in dataRows)
{
var cellList = row.ItemArray.ToList();
row.ItemArray = cellList.Select(x => x.ToString().Trim()).ToArray();
}
into one line of code so I don't have to loop through each row? Something like this but it doesn't work:
dataTable.AsEnumerable().Select(y => y.ItemArray.ToList()).Select(x => x.ToString().Trim());
If you love LINQish stype:
dataTable.AsEnumerable().ToList()
.ForEach(row =>
{
var cellList = row.ItemArray.ToList();
row.ItemArray = cellList.Select(x => x.ToString().Trim()).ToArray();
});
With linq you can't change item values finally you should run for loop (or foreach) to change fields value.
for example
var iq = obj from dataTable.asEnumerable() select new{
PersonName = a.Field<string>("PersonName"),
PersonID = a.Field<decimal>("PersonID"),
ParticipantString = a.Field<string>("DisplayString"),
PersonUserName = d.Field<string>("UserName")
}

how to select collection type navigation property's value

I have 3 tables, team(id,name) player(id,teamid,name) playerdetail(id,playerid,height,weight), the relationship between team and player is one to many, the relationship between player and playerdetail is one to one.
I want to use eager loading to load all the information and print out the name of players who is higher than 2 meters.
I have write the code below,
using (var context = new TestEntities())
{
var query = from t in context.Teams.Include("Players.PlayerDetails") select t;
foreach (var v in query)
{
Console.WriteLine(v.Players.Any(x => x.PlayerDetails.Any(y => y.Height > 200)));
}
Console.Read();
}
It prints out only true and false, how can I modify it and make it print out the name of player?
Thanks in advance
Why don't you just query the players through context.Players like below?
using (var context = new TestEntities())
{
var query = context.Players.Include("Team").Include("PlayerDetails")
.Where(p => p.Height > 200);
foreach (var v in query)
{
Console.WriteLine(v.Name);
}
Console.Read();
}

linq select from database where ID in an ArrayList

I have an array-list that contains some UserID.
I need a query like this:
vat tmp= users.select(a=> a.UserID in (arraylist));
what can I do?
If it's actually in an ArrayList, you should create a List<T> or array first. Then you can use Contains:
// Use the appropriate type, of course.
var ids = arraylist.Cast<string>().ToList();
var tmp = users.Select(a => ids.Contains(a.UserID));
While using Contains on the plain ArrayList may well compile, I would expect it to fail at execution time, assuming users is an IQueryable<>.
List<long> list =new List<long>();
var selected = from n in users where list.Contains(n.ID) select n ;
OR
var selected = users.Where(a=> list.Contains(a.ID)).ToList();
This is the solution I used.
public static IEnumerable<SettingModel> GetSettingBySettingKeys(params string[] settingKey)
{
using (var db = new BoxCoreModelEntities())
{
foreach (var key in settingKey)
{
var key1 = key;
yield return Map(db.Settings.Where(s => s.SettingKey == key1).First());
}
}
}

Iterate data in anonymous variable

I have a DataTable containing some data, and I am going to fetch some data from it using Linq to datatable.
The query looks like this:
var requiredData=dt.AsEnumerable()
.Where(row => row.Field<byte>("id") == 1)
.Select(x=> {
id = x.Field<int>("id"),
YYYYY = x.Field<string>("ColumnName2"),
ZZZZZ = x.Field<string>("ColumnName3")
}
);
Now, Please how do I iterate-through "requiredData"?
You could use a foreach loop:
foreach (var item in requiredData)
{
// TODO: use item.id, item.YYYYY and item.ZZZZZ here
}

LINQ refactoring help needed

How would you refactor this code, with respect to the LINQ? I'm new to LINQ and haven't quite have a good handle on more complex queries (nested, grouping).
Can all of these three statements and foreach loop been converted into one LINQ statement?
void AddSeries(Series series, int phraseId)
{
using (var db = Database.Instance)
{
foreach (var date in db.Ad.Select(ad => ad.DateTime.Date).Distinct())
{
var phraseCount = (from pc in db.PhraseCount
where pc.DateTime.Date == date &&
pc.PhraseId == phraseId
select pc.Count).SingleOrDefault();
var adCount = db.Ad.Where(ad => ad.DateTime.Date == date).Count();
series.Add(date, phraseCount / adCount);
}
}
}
Here's my first shot. Hard without having your model.
var q = from ad in db.Ad
group ad by ad.DateTime.Date into g
select new
{
AdCount = g.Count(),
Date = g.Key,
PhraseCount = (from pc in db.PhraseCount
where pc.DateTime.Date == g.Key
&& pc.PhraseId == phraseId
select pc).Count()
}

Resources