I like to trim following string but there is an error:
var getClients = (from c in GeneralUtillities)
orderby c.Client_Name
select new
{
c.Client_Name.Trim(),
c.Client_Code,
});
Thnx
You need to provide names for the Anonymous type object properties
var getClients = (from c in GeneralUtillities)
orderby c.Client_Name
select new
{
Name = c.Client_Name.Trim(),
Client_Code = c.Client_Code
};
If you don't provide a name in an anonymous type property, it attempts to use the property name of the value it's being assigned. As you've invoked a method on the property, it can't resolve the name. You need to specify it:
var getClients = (from c in GeneralUtillities)
orderby c.Client_Name
select new
{
Client_Name = c.Client_Name.Trim(),
c.Client_Code,
});
I see 3 things - since you didn't specify the error I'm not certain what the true problem is, but here are some guesses:
You've got a closing paren at the end of GeneralUtillities which is a syntax error
You don't specify a name for the first field in the anonymous type
Linq-to-Entities may not support the use of Trim
Here's an alternative:
var getClients = (from c in GeneralUtillities
orderby c.Client_Name)
.AsEnumerable()
.Select (c => new
{
Client_Name = c.Client_Name.Trim(),
Client_Code = c.Client_Code, // for readability, not necessary
});
The name of the property of an anonymous type must be known at compile time.
var getClients = (from c in GeneralUtillities)
orderby c.Client_Name
select new
{
Name= c.Client_Name.Trim(),
Code = c.Client_Code,
});
var getClients =
(from c in GeneralUtillities.a.data
orderby c.Client_Name
select new
{
c.ID_Client,
c.Client_Name,
});
This is right code, so the problem is to trim Client Name to have no space in start and end.
Related
I'm trying to output data like this:
[[...],[...],[...],[...]]
But my query gives me this result:
[{...},{...},{...},{...}]
Here is my query:
var result = (from c in displayedCompanies
group c by new { c.CodigoDeVenta } into s
select new{
ID = Convert.ToString(i++),
s.Key.CodigoDeVenta,
TotalInv = s.Sum(x => x.Inventario)
}).ToArray();
I've tried some options like the following but it's wrong:
var result = (from c in displayedCompanies
group c by new { c.CodigoDeVenta } into s
select new [] {
ID = Convert.ToString(i++),
s.Key.CodigoDeVenta,
TotalInv = s.Sum(x => x.Inventario)
}).ToArray();
Note that the diference is the [] in select new []. I used to have a query before I implemented the "group by" that worked correctly, but after I added the group by this does not work anymore.
Thanks!
Setting aside the grouping, focusing only on the syntax, your anonymous array (new [] {...}) should be fine.
However, it appears that you're trying to assign Id = and TotalInv = which looks like a leftover from the previous revision where you were selecting an anonymous object.
So, instead, you should be able to drop the member identifiers and select just the values you want in the child arrays:
select new [] {
Convert.ToString(i++),
s.Key.CodigoDeVenta,
s.Sum(x => x.Inventario)
}
I want to get all items in the database using LINQ where the Title starts with special characters or number, I already tried the code below but it's not working.
Thanks
result = (from asset in _db.Query<Asset>()
where !char.IsLetter(asset.Title[0])
select new AssociatedItem { Id = asset.AssetId, Title = asset.Title, Type = Constants.FeedbackTypes.ASSET }).ToList();
That's because char.IsLetter is not a dbFunction.
You can apply where after converting the results ToList()
result = (from asset in _db.Query<Asset>()
select new AssociatedItem { Id = asset.AssetId, Title = asset.Title, Type = Constants.FeedbackTypes.ASSET }).ToList()
.Where(a => !char.IsLetter(a.Title[0])).ToList();
PS: Try to identify some other where clause for the db query to limit the results.
I'd give the SqlMethods class from the System.Data.Linq.SqlClient namespace a shot.
result = (from asset in _db.Query<Asset>()
where !SqlMethods.Like(asset.Title, "[a-Z]%")
select
new AssociatedItem
{
Id = asset.AssetId,
Title = asset.Title,
Type = Constants.FeedbackTypes.ASSET
}).ToList();
Im getting an error in my C# project which is causing me a headache. The error is:
Cannot implicitly convert type 'System.Collecitons.Generic.IEnumbrable<Models.tbl_station>'
to Models.tbl_station An explicit conversion exists(are u missing a cast)
Here is my code.
var results =
(from p in db.tbl_pageDetail
group p by new { p.station_id, p.category_id } into g
let pageno = (from i in g select i.pageNo)
let station = (from i in g select i.tbl_station)
select new
{
g.Key.category_id,
g.Key.station_id,
pageno,
station
}).ToList();
var data =
results.Select(x =>
new tbl_pageDetail
{
category_id = x.category_id,
pageNo = string.Join(", ", x.pageno),
station_id = x.station_id,
tbl_station = x.station // Here i getting error
});
return View(data);
Your error message is self explanatory! x.station is an IEnumerable and you are trying to store that in tbl_station. Just consider your own example:-
pageNo = string.Join(", ", x.pageno)
Here since x.pageno was an IEnumerable you were able to pass it to Join method to create a single item, Same is the case with tbl_station = x.station, here you have a List so you cannot store that into a single object, you either need an IEnumerable of tbl_station or you need to fetch a single item from x.station by using FirstOrDefault() or SingleOrDefault().
I'm passing from the controller an array generated by the next code:
public ActionResult GetClasses(bool ajax, string kingdom)
{
int _kingdom = _taxon.getKingdom(kingdom);
var query = (from c in vwAnimalsTaxon.All()
orderby c.ClaName
select new { taxRecID = c.ClaRecID, taxName = c.ClaName }).Distinct();
return Json(query, JsonRequestBehavior.AllowGet);
}
The query List should be ordered, but it doesn't work, I get the names of the classes ordered wrong in the array, because I've seen it debugging that the names are not ordered.The view is just a dropdownbox loaded automatically, so I'm almost sure the problem is with the action. Do you see anything wrong?Am I missing something?
I think gmcalab is almost there. The reason it's not working is that Distinct blows away the ordering. So you need Distinct THEN OrderBy. But this means you have to sort by the new attribute name:
var query = (from c in vwAnimalsTaxon.All()
select new { taxRecID = c.ClaRecID, taxName = c.ClaName }
).Distinct().OrderBy(t => t.taxName);
Give this a try:
var query = (from c in vwAnimalsTaxon.All()
select new { taxRecID = c.ClaRecID, taxName = c.ClaName }
).Distinct().OrdeyBy(c => c.ClaName);
In LINQ the Distinct method makes no guarantees about the order of results. In many cases the Distinct causes the OrderBy method to get optimized away. So it's necessary to do the OrderBy last, after the Distinct.
var query = (from c in vwAnimalsTaxon.All()
select new { taxRecID = c.ClaRecID, taxName = c.ClaName })
.Distinct()
.OrderBy(c => c.ClaName);
The select will also blow away the sorting. So either Distinct or Select needs orderby after.
I'm new to Linq to Entity stuff, so I don't know if what I'm doing is the best approach.
When I do a query like this it compiles, but throws an error that it doesn't recognize the method GetItemSummaries. Looking it up, this seems to be because it doesn't like a custom method inside the query.
return (from c in _entity.Category
from i in c.Items
orderby c.Id, i.Id descending
select new CategoryDto
{
Id = c.Id,
Name = c.Name,
Items = GetItemSummaries(c)
}).ToList();
private IEnumerable<ItemSummary> GetItemSummaries(CategoryDto c)
{
return (from i in c.Items
select new ItemSummary
{
// Assignment stuff
}).ToList();
}
How would I combine this into a single query since I can't call a custom method?
I tried just replacing the method call with the actual query, but then that complains that ItemSummary isn't recognized instead of complaining that the method name isn't recognized. Is there any way to do this? (Or a better way?)
You should be able to do the following:
return (
from c in _entity.Category
orderby c.Id descending
select new CategoryDto
{
Id = c.Id,
Name = c.Name,
Items = (
from i in c.Items
order by i.Id descending
select new ItemSummary
{
// Assignment stuff
}
)
}).ToList();
It's just a matter of making sure that ItemSummary is public so that it's visible to the query.
If it's just a Dto though, you could use an anon type, eg:
from i in c.Items
order by i.Id descending
select new
{
Id = i.Id,
Name = i.Name
}
This creates a type with the 'Id' and 'Name' properties. All depends what your consuming code needs :)
Try making the GetItemSummaries method an extension method on the Category class
private static IEnumerable<ItemSummary> GetItemSummaries(this Category c)
and then call it with
select new Category
{
Id = c.Id,
Name = c.Name,
Items = c.GetItemSummaries()
}).ToList();
Marc