I have been trying to get the results for the query below and add the data into a list so that I can remove duplicates
var w = sqlData.AsEnumerable().Where(data => data.Field<String>("slideNo") == "5")
.Select(data=> data.Field<String>("QuestionStartText"));
this information give out data based on column and I want to go though the variable and put each inderviual string into a lsit
return sqlData
.AsEnumerable()
.Where(data => data.Field<String>("slideNo") == "5"))
.Select(data=> data.Field<String>("QuestionStartText"))
.Distinct()
.ToList();
Try This:-
var w = sqlData.AsEnumerable().Where(data => data.Field<String>("slideNo") == "5")
.Select(data=> data.Field<String>("QuestionStartText")).Distinct().ToList();
Related
Need your help: I have Datable that has datarows like :
test1
test1:1
test1:1:1
test1:2
I need to select only rows that contain ":" only once.
Result should be like :
test1:1
test1:2
Any ideas how to make it ??
I stuck after :
var result = dTable.AsEnumerable().Where(dr => dr.Field<string>("Name").Contains(":"));
,where "Name" is a column Name.
Thank you in advance
var result = dTable.AsEnumerable()
.Where(dr => dr.Field<string>("Name").Count(z=>z==':')==1);
or
var result = dTable.AsEnumerable()
.Where(dr => dr.Field<string>("Name").Where(z=>z==':').Count()==1);
or
var result = dTable.AsEnumerable()
.Where(dr => dr.Field<string>("Name").IndexOf(':') == dr.Field<string>("Name").LastIndexOf(':') && dr.Field<string>("Name").Contains(":"));
You can convert the content of the Field to a Char array and then count the number of times you get the ':'
var result = dt.AsEnumerable()
.Where(dr => dr.Field<string>("Name")
.ToCharArray()
.Count(c => c == ':') == 1);
Try this instead:
var result = dTable.AsEnumerable().Where(dr => dr.Field<string>("Name").Count(f => f == ':') == 1);
With LINQ, this is very easy using x.Count().
If you want to do it without LINQ, try this:
var result = dTable.AsEnumerable().Where(dr => dr.Field<string>("Name").Split(':').Length - 1 == 1);
See the top answer at this other Stack Overflow question, which has something similar. I don't usually use LINQ, so forgive me if my code doesn't work. Hope this helps!
I have a datatable that populates a list. The datatable has 3 columns, 'unit', 'clientId' and 'incident'. The incident could fx be '1. rate paid' or 'resold'.
I'm trying to exclude unit's from the list where any of it's incident is = 'resold', something like:
.Where(r => r.Field<string>("clientId") & r => r.Field<string>("unit").any() != resold))
This is my code:
var units = new List<string>();
if (showIndividualData == true)
{
units = dt.AsEnumerable()
.Where(r => r.Field<string>("clientId") == clientId)
.Select(r => r.Field<string>("unit"))
.Distinct()
.ToList();
}
else
{
units = dt.AsEnumerable()
.Where(r => r.Field<string>("clientId").Length > 0)
.Select(r => r.Field<string>("unit"))
.Distinct()
.ToList();
}
Here's a sample of a working solution. I assumed all string fields but you get the idea. I have also commented each line to explain the process of the linq for easier understanding if you have difficulties.
// some dummy table i assume you have filled
var dt = new DataTable();
// arbitrary client if, you can alter the beginning of the linq for your multi client query somehow
var clientId = "some client";
// the important linq syntax
var units = dt.Rows.Cast<DataRow>() // as datarow array
.Where(dr => dr["ClientId"].ToString() == clientId) // filter client
.GroupBy(dr => dr["unit"].ToString()) // group by unit for easy conditional after
.Where(grp => grp.ToList().All(dr => dr["incident"].ToString() != "resold")) // filter each group and keep only those where all the records are not "resold"
.Select(grp => grp.Key).ToList(); // select the key of the groups which is the "unit" column and already filtered
One can use the Any with the predicate you mentioned...just change it to
&& r.Field<string>("unit").Any(itm => !itm.Equals(resold))
and that will help discriminate it in your Where clause.
I have the following Entity Framework function that it joining a table to a list. Each item in serviceSuburbList contains two ints, ServiceId and SuburbId.
public List<SearchResults> GetSearchResultsList(List<ServiceSuburbPair> serviceSuburbList)
{
var srtList = new List<SearchResults>();
srtList = DataContext.Set<SearchResults>()
.AsEnumerable()
.Where(x => serviceSuburbList.Any(m => m.ServiceId == x.ServiceId &&
m.SuburbId == x.SuburbId))
.ToList();
return srtList;
}
Obviously that AsEnumerable is killing my performance. I'm unsure of another way to do this. Basically, I have my SearchResults table and I want to find records that match serviceSuburbList.
If serviceSuburbList's length is not big, you can make several Unions:
var table = DataContext.Set<SearchResults>();
IQuerable<SearchResults> query = null;
foreach(var y in serviceSuburbList)
{
var temp = table.Where(x => x.ServiceId == y.ServiceId && x.SuburbId == y.SuburbId);
query = query == null ? temp : query.Union(temp);
}
var srtList = query.ToList();
Another solution - to use Z.EntityFramework.Plus.EF6 library:
var srtList = serviceSuburbList.Select(y =>
ctx.Customer.DeferredFirstOrDefault(
x => x.ServiceId == y.ServiceId && x.SuburbId == y.SuburbId
).FutureValue()
).ToList().Select(x => x.Value).Where(x => x != null).ToList();
//all queries together as a batch will be sent to database
//when first time .Value property will be requested
I have the following linq:
objfl = db.tblFl.First(t => t.sp == id && t.ProgID == sPgm);
I like to also order by id but not sure how to do this. I tried a number of different ways but was not successful
As suggested by BrokenGlass, if you want to filter by ProgID, sort by sp and retrieve the first item:
db.tblFl.Where(t => t.ProgID == sPgm)
.OrderBy(t => t.sp)
.First()
Try this
objfl = db.tblFl.Where(t => t.sp == id && t.ProgID == sPgm).OrderBy(t => t.sp);
I was wondering if there is a way to get a list of results into a list with linq to xml. If I would have the following xml for example:
<?xml version="1.0"?>
<Sports xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SportPages>
<SportPage type="test">
<LinkPage>
<IDList>
<string>1</string>
<string>2</string>
</IDList>
</LinkPage>
</SportPage>
</SportPages>
</Sports>
How could I get a list of strings from the IDList?
I'm fairly new to linq to xml so I just tried some stuff out, I'm currently at this point:
var IDs = from sportpage in xDoc.Descendants("SportPages").Descendants("SportPage")
where sportpage.Attribute("type").Value == "Karate"
select new
{
ID = sportpage.Element("LinkPage").Element("IDList").Elements("string")
};
But the var is to chaotic to read decently. Isn't there a way I could just get a list of strings from this?
Thanks
This query works - tested and verified:
var ID2 = (from sportpage in xDoc.Descendants("SportPages").Descendants("SportPage")
where sportpage.Attribute("type").Value == "Karate"
select sportpage)
.Descendants("LinkPage")
.Descendants("IDList")
.Elements("string")
.Select(d => d.Value)
.ToList();
Gives me a list of two strings, "1" and "2".
var myStrings = xDoc.Descendants("SportPage")
.Where(d => d.Attribute("type").Value == "Karate")
.Descendants("IDList")
.Descendants("string")
.Select(d => d.Value);
to see your string:
xDoc.Descendants("SportPage")
.Descendants("IDList")
.Where(d => d.Attribute("type").Value == "Karate")
.Descendants("string")
.Select(d => d.Value)
.ToList()
.ForEach(Console.WriteLine);
Do you mean this?
List<string> IDs = xDoc.Descendants("SportPages").Descendants("SportPage")
.Where( anySportPage => anySportpage.Attribute("type").Value == "Karate" )
.Select( karateSportPage => karateSportpage.Element("LinkPage").Element("IDList").Elements("string"))
.ToList();
I think the reason you find the "var" chaotic is your creation of the anonymous type with the "new" in your select. If you just select the one item you're after then the var will not be an anonymous type.
e.g.
select sportpage.Element("LinkPage").Element("IDList").Elements("string");
However, my preference would be to do that using the . notation like this.
List<string> ids = xDoc.Elements("SportPages").Elements("SportPage").Where(sportPage => sportPage.Attribute("type").Value == "Karate").Elements("LinkPage").Elements("IDList").Elements("string").Select(id => id.Value).ToList();
The biggest issue you were having was that you didn't grab the .Value from the returned element set. But here's another way to do it.
var ids = from sportPage in xDoc.Descendants("SportPage")
let attrib = sportPage.Attribute("type")
where attrib != null
let type = attrib.Value
where !string.IsNullOrEmpty(type)
&& type == "Karate"
from id in sportPage.Descendants("IDList").Elements()
select id.Value;