Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi,

how to replace value in select Query with string from resources like this example:

strings.ResourceManager.GetString(s.StartStatus, strings.Culture);


What I have tried:

i tried this :

var Query = DB1.view_items_history
            .Where(u => u.stitems_ID == itemID &&  u.OpDate >= From && u.OpDate <= To)
            .Select(s => new CLSItemsHistory
            {
                stitems_ID = s.stitems_ID,
                stitems_Status = s.stitems_Status,
                stitems_Name = s.stitems_Name,
                stitems_Type = s.stitems_Type,
                stitems_Type_name = s.stitems_Type == 0 ? strings.STOCKS : s.stitems_Type == 1 ? strings.STOCKS_WITH_SERIAL : "",
                stitems_Code = s.stitems_Code,
                stitems_NationalCode = s.stitems_NationalCode,
                DetunitID = s.DetunitID,
                UnitName = s.UnitName,
                ID = s.ID,
                mID = s.mID,
                OpType = s.OpType,
                OpName = strings.ResourceManager.GetString(s.OpType, strings.Culture),
                OpDate = s.OpDate,
                r_branche_ID = s.r_branche_ID,
                branche_Name = s.branche_Name,
                IncmQnt = s.IncmQnt,
                OutQnt = s.OutQnt,
                cost = s.cost,
                TotalCost = s.TotalCost,
                FromStore = s.FromStore,
                fstore_Name = s.fstore_Name,
                ToStore = s.ToStore,
                tstore_Name = s.tstore_Name,
                StartStatus = strings.ResourceManager.GetString(s.StartStatus, strings.Culture),
                EndStatus = strings.ResourceManager.GetString(s.EndStatus, strings.Culture)

            });


but this error appear :

System.NotSupportedException: 'LINQ to Entities does not recognize the method 'System.String GetString(System.String, System.Globalization.CultureInfo)' method, and this method cannot be translated into a store expression.'
Posted
Updated 31-May-19 3:37am

Easy enough. Assign the value of the ResourceManager.GetString call to a string variable outside of the LINQ expression, then just use the variable in place of all of your ResourceManager.GetString calls.

The reason you get that message is because the underlying database engine doesn't have a clue what a ResourceManager is or how to translate any call to it into SQL code.
 
Share this answer
 
Comments
Golden Basim 30-May-19 5:09am    
thanks , how to do your that without affect performance. i stored the results in this list
List<CLSItemsHistory> dList = new List<CLSItemsHistory>();
Dave Kreskowiak 30-May-19 12:19pm    
You can't be serious...
string osTypeString = strings.ResourceManager.GetString(s.OpType, strings.Culture);

Then you just use the osTypeString in place of your call to GetString in the LINQ statement.

The same goes for your StartStatus and EndStatus.
Golden Basim 30-May-19 12:47pm    
i'm sorry but this not clear for me.. the results have thousands of records that have Hundreds of types (that need to translated)
Dave Kreskowiak 30-May-19 14:00pm    
You are apparently looking at something complete different from what I am and from what your original question stated.

All I'm talking about is the retrieval of a string from Resources and using it in the LINQ query.

I'm not saying anything at all about your List<clsitemshistory>.
All of the code which references the ResourceManager needs to run in-memory, not in the database.

Add an .AsEnumerable() call between the .Where(...) and the .Select(...) statements to force the projection to run in-memory:
C#
var Query = DB1.view_items_history
            .Where(u => u.stitems_ID == itemID && u.OpDate >= From && u.OpDate <= To)
            .AsEnumerable()
            .Select(s => new CLSItemsHistory
            {
                stitems_ID = s.stitems_ID,
                stitems_Status = s.stitems_Status,
                stitems_Name = s.stitems_Name,
                stitems_Type = s.stitems_Type,
                stitems_Type_name = s.stitems_Type == 0 ? strings.STOCKS : s.stitems_Type == 1 ? strings.STOCKS_WITH_SERIAL : "",
                stitems_Code = s.stitems_Code,
                stitems_NationalCode = s.stitems_NationalCode,
                DetunitID = s.DetunitID,
                UnitName = s.UnitName,
                ID = s.ID,
                mID = s.mID,
                OpType = s.OpType,
                OpName = strings.ResourceManager.GetString(s.OpType, strings.Culture),
                OpDate = s.OpDate,
                r_branche_ID = s.r_branche_ID,
                branche_Name = s.branche_Name,
                IncmQnt = s.IncmQnt,
                OutQnt = s.OutQnt,
                cost = s.cost,
                TotalCost = s.TotalCost,
                FromStore = s.FromStore,
                fstore_Name = s.fstore_Name,
                ToStore = s.ToStore,
                tstore_Name = s.tstore_Name,
                StartStatus = strings.ResourceManager.GetString(s.StartStatus, strings.Culture),
                EndStatus = strings.ResourceManager.GetString(s.EndStatus, strings.Culture)
            });
 
Share this answer
 
Comments
Golden Basim 31-May-19 9:44am    
thank you , it will affect the performance ?? ,, i use skip() to fetch only limited records if i used .AsEnumerable() it will fetch all the records before Skip() ?
Richard Deeming 31-May-19 9:46am    
Yes, if you call .AsEnumerable() first, then it will fetch all records. You'll want to call OrderBy, Skip, and Take before calling AsEnumerable.
Golden Basim 31-May-19 9:47am    
thank you i will try it now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900